Skip to content

Instantly share code, notes, and snippets.

@Naveendhanaraj
Created September 4, 2018 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Naveendhanaraj/968edbdd4467609d01fd934e4788dcf9 to your computer and use it in GitHub Desktop.
Save Naveendhanaraj/968edbdd4467609d01fd934e4788dcf9 to your computer and use it in GitHub Desktop.
public class RefreshTokenClass {
private auth_response rt;
public string str {get; set;}
public string clientid {get; set;}
public string clientsecret {get; set;}
public pagereference auth_Step_1(){
String auth_url = 'https://login.salesforce.com/services/oauth2/authorize';
String params =
'?response_type=code' +
'&client_id=' + encodingUtil.urlencode(clientid,'UTF-8') +
'&redirect_uri=https://c.na53.visual.force.com/apex/RefreshToken';
pageReference pr = New PageReference(auth_url + params);
return pr.setRedirect(false);
}
public pagereference auth_Step_2(){
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(clientid,'UTF-8') +
'&client_secret='+ clientsecret +
'&redirect_uri=https://c.na53.visual.force.com/apex/RefreshToken';
system.debug('auth_Step_2>>>>>>'+params);
req.setMethod('POST');
req.setEndpoint(auth_url + params);
HTTPResponse resp = http.send(req);
rt = (auth_response)json.deserialize(resp.getBody(),auth_response.class);
system.debug('rt>>>>'+ rt );
str = rt.refresh_token;
return null ;
}
private class auth_response{
public string refresh_token;
public string access_token;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment