Advertisement
Guest User

Untitled

a guest
Feb 18th, 2017
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. //We can also store our username password in custom setting.
  2. String username='xxx';//salesforce username
  3. String password='xxxx';//EUe4eHjMxXb8UFco1SPcpsZL9';//salesforce password+securitytoken
  4. // Generating the Access Token
  5. HttpRequest req = new HttpRequest();
  6. req.setMethod('POST');
  7. req.setEndpoint('https://login.salesforce.com/services/oauth2/token');// this is the OAuth endpoint where this request will be hit
  8. req.setBody('grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password);
  9.  
  10. Http http = new Http();
  11. HTTPResponse res = http.send(req);
  12. String str = res.getBody();
  13. wrapObj = (Wrapper)Json.deserialize(str,Wrapper.class);
  14. accessToken = wrapObj.access_token;
  15. instanceUrl = wrapObj.instance_url;
  16. methodGet();
  17. }
  18.  
  19. // Retrieve the Contacts from the otehr org.
  20. public void methodGet(){
  21. HttpRequest req = new HttpRequest();
  22. req.setMethod('GET');
  23. //req.setEndpoint(wrapObj.instance_url+'/services/apexrest/Account/getAccountById?name=champaKAli');
  24.  
  25. req.setEndpoint('https://lightningwebsite-dev-ed.my.salesforce.com/services/apexrest/AccountService');
  26. req.setHeader('Authorization', 'Bearer'+wrapObj.access_token);
  27. Http http = new Http();
  28. HTTPResponse res = http.send(req);
  29.  
  30. System.debug('***Response***** ' + res.getBody());
  31. //---------------------Here ------------------
  32. /*JSONParser parser = JSON.createParser(res.getBody());
  33. do{
  34. parser.nextToken();
  35. }while(parser.hasCurrentToken() && !'records'.equals(parser.getCurrentName()));
  36. parser.nextToken();
  37. List<account> acc = (List<Account>) parser.readValueAs(List<Account>.class);*/
  38. }
  39. public Contact con {get;set;}
  40. public String s1 {get;set;}
  41. public String accessToken;
  42. public String instanceUrl;
  43. public Wrapper wrapObj{get;set;}
  44. // Wrapper Class to Store the value from the Jason.
  45. public class Wrapper{
  46. String id;
  47. String instance_url;
  48. String access_token;
  49. String issued_at;
  50. String signature;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement