Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RestResource(urlMapping = '/Account/*')
- global with sharing class AccountWS
- {
- @HttpPost
- global static String createAccount(String nam)
- {
- Account a = new Account(Name = nam);
- insert a;
- return a.Id;
- }
- }
- public class OAuth2
- {
- public String id{get;set;}
- public String issued_at{get;set;}
- public String instance_url{get;set;}
- public String signature{get;set;}
- public String access_token{get;set;}
- }
- RESTCreds__c r = RESTCreds__c.getValues('1');//Custom settings contains all credentials
- String clientId = r.ConsumerKey__c;
- String clientSecret = r.ConsumerSecret__c;
- String userName = r.Username__c;
- String password = r.Password__c;
- String url = r.URL__c;
- System.debug(clientId);
- System.debug(clientSecret);
- System.debug(userName);
- System.debug(password);
- System.debug(url);
- String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
- HTTP h = new HTTP();
- HttpRequest req = new HttpRequest();
- req.setBody(reqbody);
- req.setMethod('POST');
- req.setEndPoint(url);
- HttpResponse res = h.send(req);
- System.debug('Response : '+res);
- System.debug('Response Body : '+res.getBody());
- OAuth2 objAuthenticationInfo = (OAuth2) JSON.deserialize(res.getBody(),OAuth2.class);
- System.debug(objAuthenticationInfo);
- System.debug('Id = '+objAuthenticationInfo.id);
- System.debug('Issued At = '+objAuthenticationInfo.issued_at);
- System.debug(' Instance URL = '+objAuthenticationInfo.instance_url);
- System.debug(' Signature = '+objAuthenticationInfo.signature);
- System.debug(' Access Token = '+objAuthenticationinfo.access_token);
- String jsonstr = '{"nam":"sdsd"}';
- System.debug(jsonstr);
- if(objAuthenticationInfo.access_token != null)
- {
- Http h1 = new Http();
- HttpRequest req1 = new HttpRequest();
- req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
- req1.setHeader('Content-Type','application/json');
- req1.setHeader('accept','application/json');
- req1.setBody(jsonstr);
- req1.setMethod('POST');
- req1.setEndpoint(url+'/services/apexrest/Account');
- HttpResponse res1 = h1.send(req1);
- System.debug('Response for Payload : '+res1.getBody()); //WEIRD RESPONSE HERE
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement