Different ways for Getting Refresh Token:
1.Manually enter Client Id and Client secret in Input field.
Every Time user has to Manually Generate RefreshToken
2.Using Custom Label or Custom settings.
when the client Id/secret is placed in custom label, client Id/secret is not editable while Installing as managed package .
when the client Id/secret is placed in Custom settings, client Id/secret is editable while Installing as managed package.
Example:
The use case is to generate Refresh token manually inorder to track who is asking for Refresh Token and when the data comes in to salesforce,so inorder track all this information refresh token is generated manually.
In This Example I have used Input field to manually enter Client Id and Client Secret to get the Refresh Token.
ClientId– Consumer Key from Connected Apps.
ClientSecret- Consumer Secret from Connected Apps.
VISUALFORCE PAGE WITH SLDS:
<apex:page controller="RefreshTokenClass" > | |
<apex:slds /> | |
<apex:form > | |
<div class="slds-form slds-form_stacked"> | |
<div class="slds-form-element slds-size_2-of-4"> | |
<label class="slds-form-element__label" for="input-id-01"><b>Client Id</b></label> | |
<div class="slds-form-element__control"> | |
<apex:inputText id="clienti" styleClass="slds-input" value="{!clientid}"/> | |
</div> | |
</div> | |
<div class="slds-form-element slds-size_2-of-4"> | |
<label class="slds-form-element__label" for="input-id-01"><b>Client Secret</b></label> | |
<div class="slds-form-element__control"> | |
<apex:inputText id="clientsecr" styleclass="slds-input" value="{!clientsecret}" /> | |
</div> | |
</div> | |
</div> | |
<br/> | |
<apex:commandButton action="{!auth_step_1}" value="Step1:Get Authorization Code"/> | |
<apex:commandButton action="{!auth_step_2}" value="Step2:Get RefreshToken"/> | |
<br/> | |
<div style="display:{!IF($CurrentPage.parameters.code != Null && str = Null ,'block','none')}"> | |
<label><b>Authorization Code:</b></label> | |
<div class="slds-page-header slds-p-top_medium"> | |
<b>{!$CurrentPage.parameters.code}</b> | |
</div> | |
</div> | |
<div style="display:{!IF(str = Null, 'none','block')}"> | |
<label><b>Refresh Token:</b></label> | |
<div class="slds-page-header slds-p-top_medium" style="display:{!IF(str = Null,'none','block')}"> | |
<b>{!str}</b> | |
</div> | |
</div> | |
</apex:form> | |
</apex:page> |
APEX CLASS:
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; | |
} | |
} |
Lets Learn Together!!!