Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. public class OauthIntegrationController {
  2.  
  3. public Static String accessToken = '';
  4.  
  5. public static void oauthLogin(String loginUri, String clientId,
  6. String clientSecret,String redirectUri){
  7.  
  8.  
  9. HttpRequest req = new HttpRequest();
  10.  
  11. req.setMethod('POST');
  12. req.setEndpoint(loginUri+'/services/oauth2/token');
  13.  
  14. req.setBody('grant_type=authorization_code' +
  15. // req.setBody('response_type = code' +
  16. '&client_id=' + clientId +
  17. '&redirect_uri='+redirectUri+
  18. '&client_secret='+clientSecret);
  19.  
  20. /* '&username=' + EncodingUtil.urlEncode(username, 'UTF-8') +
  21. '&password=' + EncodingUtil.urlEncode(password, 'UTF-8'));*/
  22.  
  23. Http http = new Http();
  24.  
  25. HTTPResponse res = http.send(req);
  26.  
  27. System.debug('BODY: '+res.getBody());
  28. System.debug('STATUS:'+res.getStatus());
  29. System.debug('STATUS_CODE:'+res.getStatusCode());
  30. JSONParser parser = JSON.createParser(res.getBody());
  31.  
  32. accessToken = '';
  33.  
  34. while (parser.nextToken() != null) {
  35.  
  36. if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
  37.  
  38. (parser.getText() == 'access_token')) {
  39. parser.nextToken();
  40. accessToken = parser.getText();
  41. }
  42.  
  43. }
  44.  
  45. restTest(accessToken );
  46.  
  47. }
  48.  
  49.  
  50. public static String restTest(String token ) {
  51.  
  52. HttpRequest req = new HttpRequest();
  53.  
  54. req.setMethod('GET');
  55. req.setEndpoint('https://ap2.salesforce.com'+'/services/apexrest/TestRestResponseController');
  56. req.setHeader('Authorization', 'OAuth '+token);
  57.  
  58. Http http = new Http();
  59.  
  60. HTTPResponse res = http.send(req);
  61.  
  62. System.debug('BODY: '+res.getBody());
  63. System.debug('STATUS:'+res.getStatus());
  64. System.debug('STATUS_CODE:'+res.getStatusCode());
  65.  
  66. return res.getBody();
  67. }
  68.  
  69. public class oAuth_Controller{
  70.  
  71. private auth_response rt;
  72.  
  73. public pagereference auth_Step_1(){
  74.  
  75.  
  76. String auth_url = 'https://login.salesforce.com/services/oauth2/authorize';
  77. String params =
  78. '?response_type=code' +
  79. '&client_id=' + encodingUtil.urlencode('YOURCLIENTID','UTF-8') +
  80. '&redirect_uri=https://na1.salesforce.com/apex/{YOURVFPAGE}' + '&prompt=consent' +
  81. '&scope=' + encodingUtil.URLEncode('full refresh_token','UTF-8') +
  82. '&state=step2';
  83. pageReference pr = New PageReference(auth_url + params);
  84. return pr;
  85. }
  86.  
  87. public pagereference auth_Step_2(){
  88.  
  89. if(apexPages.currentPage().getParameters().get('state') != 'step2')
  90. return null;
  91.  
  92.  
  93. HttpRequest req = new HttpRequest();
  94. Http http = new Http();
  95.  
  96. String auth_url = 'https://login.salesforce.com/services/oauth2/token';
  97. String params =
  98. '?code=' + apexPages.currentPage().getParameters().get('code') +
  99. '&grant_type=authorization_code' +
  100. '&client_id=' + encodingUtil.urlencode('YOURCLIENTID','UTF-8') +
  101. '&client_secret=YOURSECRET' +
  102. '&redirect_uri=https://login.salesforce.com/apex/YOURVFPAGENAME';
  103.  
  104. req.setMethod('POST');
  105. req.setEndpoint(auth_url + params);
  106.  
  107. HTTPResponse resp = http.send(req);
  108.  
  109. rt = (auth_response)json.deserialize(resp.getBody(),auth_response.class);
  110. //Do something with the results
  111. return null;
  112.  
  113. }
  114.  
  115. public void getnewtoken(){
  116.  
  117. HttpRequest req = new HttpRequest();
  118. Http http = new Http();
  119.  
  120. String auth_url = 'https://login.salesforce.com/services/oauth2/token';
  121. String params =
  122.  
  123. '?grant_type=refresh_token' +
  124. '&client_id=' + encodingUtil.urlencode('YOURCLIENTID','UTF-8') +
  125. '&client_secret=YOURSECRET' +
  126. '&refresh_token=' + encodingUtil.URLEncode(YOURREFRESHTOKEN,'UTF-8');
  127.  
  128. req.setMethod('POST');
  129. req.setEndpoint(auth_url + params);
  130.  
  131. HTTPResponse resp = http.send(req);
  132.  
  133.  
  134. }
  135.  
  136.  
  137.  
  138. private class auth_response{
  139.  
  140. public string refresh_token;
  141. public string access_token;
  142.  
  143. }
  144. }
  145.  
  146. <apex:page controller="oAuth_Controller" action="{!auth_step_2}">
  147. <apex:form >
  148. <apex:commandButton action="{!auth_step_1}" value="Start oAuth" rerender="msgs"/>
  149. <apex:commandButton action="{!getNewToken}" value="Get new Token" rerender="msgs"/>
  150. </apex:form>
  151.  
  152. <apex:outPutPanel id="msgs">
  153. <h1>Congratulations</h1>
  154. {!$CurrentPage.parameters.access_token}
  155. </apex:outPutPanel>
  156. </apex:page>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement