Advertisement
Guest User

Untitled

a guest
May 12th, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. /*End point Url to web service callout*/
  2. private final static String ENP_POINT_URL ='https://login.salesforce.com/services/oauth2/token';
  3. //For development and production https://login.salesforce.com/services/oauth2/token
  4. //And for sandbox https://test.salesforce.com/services/oauth2/token
  5. private final static String CONSUMER_KEY = 'Your_Org_Consumer_Key';
  6. private final static String CONSUMER_SECRET = 'Your_Org_Consumer_Secret';
  7. private final static String USERNAME = 'Your_Username';
  8. private final static String PASSWORD = 'Your_Password';
  9. private final static String REQUEST_BODY = 'grant_type=password&client_id={0}&client_secret=
  10. {1}&username={2}&password={3}';
  11.  
  12.  
  13.  
  14.  
  15. /*To generate Access token Method*/
  16. private static OAuth getAccessToken(){
  17. try{
  18. HttpRequest req = new HttpRequest();
  19. req.setEndpoint(ENP_POINT_URL);
  20. req.setMethod('POST');
  21. Blob headerValue = Blob.valueOf(USERNAME + ':' + PASSWORD);
  22. String authorizationHeader = 'BASIC ' +
  23. EncodingUtil.base64Encode(headerValue);
  24. req.setHeader('Authorization', authorizationHeader);
  25. req.setBody(String.format(REQUEST_BODY ,new string[]{CONSUMER_KEY,CONSUMER_SECRET,
  26. USERNAME,PASSWORD}));
  27. req.setTimeout(60000);
  28. Http http = new Http();
  29. HttpResponse res = http.send(req);
  30. OAuth objAuthenticationInfo = (OAuth)JSON.deserialize(res.getbody(), OAuth.class);
  31. return objAuthenticationInfo;
  32. }catch(CallOutException ce){
  33. throw ce;
  34. }
  35. return null;
  36. }
  37.  
  38. /*To get Access token property*/
  39. public static OAuth authenticationDetail{
  40. get{
  41. if(authenticationDetail == null){
  42. authenticationDetail = getAccessToken();
  43. }
  44. return authenticationDetail;
  45. }set;
  46. }
  47.  
  48. /*To get aouthentication detail Wrapper*/
  49. public class OAuth{
  50. public String id{get;set;}
  51. public String issued_at{get;set;}
  52. public String instance_url{get;set;}
  53. public String signature{get;set;}
  54. public String access_token{get;set;}
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement