Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. 09:34:07:493 USER_DEBUG [63]|DEBUG|Response for Payload : [{"errorCode":"URL_NOT_RESET","message":"Destination URL not reset. The URL returned from login must be set"}]
  2.  
  3. public class OAuth2
  4. {
  5.  
  6. public String id{get;set;}
  7. public String issued_at{get;set;}
  8. public String instance_url{get;set;}
  9. public String signature{get;set;}
  10. public String access_token{get;set;}
  11.  
  12. }
  13.  
  14. RESTCreds__c r = RESTCreds__c.getValues('1');
  15.  
  16. String clientId = r.ConsumerKey__c;
  17. String clientSecret = r.ConsumerSecret__c;
  18. String userName = r.Username__c;
  19. String password = r.Password__c;
  20. String url = r.URL__c;
  21. System.debug(clientId);
  22. System.debug(clientSecret);
  23. System.debug(userName);
  24. System.debug(password);
  25. System.debug(url);
  26.  
  27. String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
  28.  
  29. HTTP h = new HTTP();
  30. HttpRequest req = new HttpRequest();
  31. req.setBody(reqbody);
  32. req.setMethod('POST');
  33. req.setEndPoint(url);
  34.  
  35. HttpResponse res = h.send(req);
  36. System.debug('Response : '+res);
  37. System.debug('Response Body : '+res.getBody());
  38.  
  39. OAuth2 objAuthenticationInfo = (OAuth2) JSON.deserialize(res.getBody(),OAuth2.class);
  40. System.debug(objAuthenticationInfo);
  41. System.debug('Id = '+objAuthenticationInfo.id);
  42. System.debug('Issued At = '+objAuthenticationInfo.issued_at);
  43. System.debug(' Instance URL = '+objAuthenticationInfo.instance_url);
  44. System.debug(' Signature = '+objAuthenticationInfo.signature);
  45. System.debug(' Access Token = '+objAuthenticationinfo.access_token);
  46.  
  47.  
  48.  
  49.  
  50. String jsonstr = '{"nam":"Test 1"}';
  51. System.debug(jsonstr);
  52.  
  53.  
  54. if(objAuthenticationInfo.access_token != null)
  55. {
  56. Http h1 = new Http();
  57. HttpRequest req1 = new HttpRequest();
  58. req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
  59. req1.setHeader('Content-Type','application/json');
  60. req1.setHeader('accept','application/json');
  61. req1.setBody(jsonstr);
  62. req1.setMethod('POST');
  63. req1.setEndpoint('https://login.salesforce.com/services/apexrest/Account');
  64. HttpResponse res1 = h1.send(req1);
  65. System.debug('Response for Payload : '+res1.getBody());
  66.  
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement