daily pastebin goal
84%
SHARE
TWEET

Untitled

a guest Jan 28th, 2016 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @RestResource(urlMapping = '/Account/*')
  2. global with sharing class AccountWS
  3. {
  4.  
  5.  
  6. @HttpPost
  7. global static String createAccount(String nam)
  8. {
  9.  
  10. Account a = new Account(Name = nam);
  11. insert a;
  12. return a.Id;
  13.  
  14. }
  15.  
  16. }
  17.    
  18. public class OAuth2
  19. {
  20.  
  21.     public String id{get;set;}
  22.     public String issued_at{get;set;}
  23.     public String instance_url{get;set;}
  24.     public String signature{get;set;}
  25.     public String access_token{get;set;}
  26.  
  27. }
  28.  
  29. RESTCreds__c r = RESTCreds__c.getValues('1');//Custom settings contains all credentials
  30.  
  31. String clientId = r.ConsumerKey__c;
  32. String clientSecret = r.ConsumerSecret__c;
  33. String userName = r.Username__c;
  34. String password = r.Password__c;
  35. String url = r.URL__c;
  36. System.debug(clientId);
  37. System.debug(clientSecret);
  38. System.debug(userName);
  39. System.debug(password);
  40. System.debug(url);
  41.  
  42. String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
  43.  
  44. HTTP h = new HTTP();
  45. HttpRequest req = new HttpRequest();
  46. req.setBody(reqbody);
  47. req.setMethod('POST');
  48. req.setEndPoint(url);
  49.  
  50. HttpResponse res = h.send(req);
  51. System.debug('Response : '+res);
  52. System.debug('Response Body : '+res.getBody());
  53.  
  54. OAuth2 objAuthenticationInfo = (OAuth2) JSON.deserialize(res.getBody(),OAuth2.class);
  55. System.debug(objAuthenticationInfo);
  56. System.debug('Id = '+objAuthenticationInfo.id);
  57. System.debug('Issued At = '+objAuthenticationInfo.issued_at);
  58. System.debug(' Instance URL = '+objAuthenticationInfo.instance_url);
  59. System.debug(' Signature = '+objAuthenticationInfo.signature);
  60. System.debug(' Access Token = '+objAuthenticationinfo.access_token);
  61.  
  62.  
  63.  
  64. String jsonstr = '{"nam":"sdsd"}';
  65. System.debug(jsonstr);
  66.  
  67.  
  68. if(objAuthenticationInfo.access_token != null)
  69. {
  70.     Http h1 = new Http();
  71.     HttpRequest req1 = new HttpRequest();
  72.     req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
  73.     req1.setHeader('Content-Type','application/json');
  74.     req1.setHeader('accept','application/json');
  75.     req1.setBody(jsonstr);
  76.     req1.setMethod('POST');
  77.     req1.setEndpoint(url+'/services/apexrest/Account');
  78.     HttpResponse res1 = h1.send(req1);
  79.     System.debug('Response for Payload : '+res1.getBody()); //WEIRD RESPONSE HERE
  80.  
  81.  
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top