Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class OauthIntegrationController {
- public Static String accessToken = '';
- public static void oauthLogin(String loginUri, String clientId,
- String clientSecret,String redirectUri){
- HttpRequest req = new HttpRequest();
- req.setMethod('POST');
- req.setEndpoint(loginUri+'/services/oauth2/token');
- req.setBody('grant_type=authorization_code' +
- // req.setBody('response_type = code' +
- '&client_id=' + clientId +
- '&redirect_uri='+redirectUri+
- '&client_secret='+clientSecret);
- /* '&username=' + EncodingUtil.urlEncode(username, 'UTF-8') +
- '&password=' + EncodingUtil.urlEncode(password, 'UTF-8'));*/
- Http http = new Http();
- HTTPResponse res = http.send(req);
- System.debug('BODY: '+res.getBody());
- System.debug('STATUS:'+res.getStatus());
- System.debug('STATUS_CODE:'+res.getStatusCode());
- JSONParser parser = JSON.createParser(res.getBody());
- accessToken = '';
- while (parser.nextToken() != null) {
- if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
- (parser.getText() == 'access_token')) {
- parser.nextToken();
- accessToken = parser.getText();
- }
- }
- restTest(accessToken );
- }
- public static String restTest(String token ) {
- HttpRequest req = new HttpRequest();
- req.setMethod('GET');
- req.setEndpoint('https://ap2.salesforce.com'+'/services/apexrest/TestRestResponseController');
- req.setHeader('Authorization', 'OAuth '+token);
- Http http = new Http();
- HTTPResponse res = http.send(req);
- System.debug('BODY: '+res.getBody());
- System.debug('STATUS:'+res.getStatus());
- System.debug('STATUS_CODE:'+res.getStatusCode());
- return res.getBody();
- }
- public class oAuth_Controller{
- private auth_response rt;
- public pagereference auth_Step_1(){
- String auth_url = 'https://login.salesforce.com/services/oauth2/authorize';
- String params =
- '?response_type=code' +
- '&client_id=' + encodingUtil.urlencode('YOURCLIENTID','UTF-8') +
- '&redirect_uri=https://na1.salesforce.com/apex/{YOURVFPAGE}' + '&prompt=consent' +
- '&scope=' + encodingUtil.URLEncode('full refresh_token','UTF-8') +
- '&state=step2';
- pageReference pr = New PageReference(auth_url + params);
- return pr;
- }
- public pagereference auth_Step_2(){
- if(apexPages.currentPage().getParameters().get('state') != 'step2')
- return null;
- HttpRequest req = new HttpRequest();
- Http http = new Http();
- String auth_url = 'https://login.salesforce.com/services/oauth2/token';
- String params =
- '?code=' + apexPages.currentPage().getParameters().get('code') +
- '&grant_type=authorization_code' +
- '&client_id=' + encodingUtil.urlencode('YOURCLIENTID','UTF-8') +
- '&client_secret=YOURSECRET' +
- '&redirect_uri=https://login.salesforce.com/apex/YOURVFPAGENAME';
- req.setMethod('POST');
- req.setEndpoint(auth_url + params);
- HTTPResponse resp = http.send(req);
- rt = (auth_response)json.deserialize(resp.getBody(),auth_response.class);
- //Do something with the results
- return null;
- }
- public void getnewtoken(){
- HttpRequest req = new HttpRequest();
- Http http = new Http();
- String auth_url = 'https://login.salesforce.com/services/oauth2/token';
- String params =
- '?grant_type=refresh_token' +
- '&client_id=' + encodingUtil.urlencode('YOURCLIENTID','UTF-8') +
- '&client_secret=YOURSECRET' +
- '&refresh_token=' + encodingUtil.URLEncode(YOURREFRESHTOKEN,'UTF-8');
- req.setMethod('POST');
- req.setEndpoint(auth_url + params);
- HTTPResponse resp = http.send(req);
- }
- private class auth_response{
- public string refresh_token;
- public string access_token;
- }
- }
- <apex:page controller="oAuth_Controller" action="{!auth_step_2}">
- <apex:form >
- <apex:commandButton action="{!auth_step_1}" value="Start oAuth" rerender="msgs"/>
- <apex:commandButton action="{!getNewToken}" value="Get new Token" rerender="msgs"/>
- </apex:form>
- <apex:outPutPanel id="msgs">
- <h1>Congratulations</h1>
- {!$CurrentPage.parameters.access_token}
- </apex:outPutPanel>
- </apex:page>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement