ACCESS TOKEN USING OAUTH 2.0 IN SALESFORCE
OAuth (Open Authorization) is an open protocol to allow secure API authorization in a simple and standardized way from desktop and web applications. The Force.com platform implements the OAuth 2.0 Authorization Framework, so users can authorize applications to access Force.com resources (via the Force.com REST and SOAP Web Service APIs) or Chatter resources (via the Chatter REST API) on their behalf without revealing their passwords or other credentials to those applications. Alternatively, applications can directly authenticate to access the same resources without the presence of an end user.
In this blog, I will be specifying different steps which we need to perform in order to generate Access token for Salesforce org. We will be using 2 different developer org. In org 1, we will be writing all code to generate access token for another org.
In order to access token from different org and storing different required information, we will create custom object (External_Application__c) and create different fields mentioned below:
Field Label
|
Field Name
|
Data Type
|
Access Token
|
Access_Token__c
|
Text Area(255)
|
Application Name
|
Application_Name__c
|
Text(255) (Unique Case Insensitive)
|
Authorization Server Response
|
Authorization_Server_Response__c
|
Long Text Area(32768)
|
Callback URL
|
Callback_URL__c
|
Text Area(255)
|
Client ID
|
Client_ID__c
|
Text Area(255)
|
Consumer secret
|
Consumer_Key__c
|
Text Area(255)
|
ID
|
ID__c
|
Text Area(255)
|
Instance URL
|
Instance_URL__c
|
Text Area(255)
|
Issued at
|
Issued_at__c
|
Text Area(255)
|
Outh Code
|
Outh_Code__c
|
Text Area(255)
|
Refresh_Token
|
Refresh_Token__c
|
Text Area(255)
|
Salesforce Domain
|
Salesforce_Domain__c
|
Text(255)
|
Scope
|
Scope__c
|
Text(255)
|
Signature
|
Signature__c
|
Text Area(255)
|
Different steps involved in order to get access token:
Login to developer organization (org 2) for which you want to generate access token.
- Navigate to Setup ➤ Create ➤ Apps, and in the Connected Apps section, click New to create a new connected app and click Enable OAuth Settings to open the API section.
- Specify name (here I am specifying "Rest Playground"), check enable OAuth settings checkbox and specify callback URL (in my case- https://xxx.salesforce.com/apex/WebServerAuthentication?AppName=SunilKumar04). here xxx refer to domain name for example ap1,ap2 etc.
- You may leave “Selected OAuth Scopes” blank.
- Click on Save. You will get consumer key and consumer secret key. Copy these 2 keys values and store it in notepad.
Now login to developer organization (org 1) where you will be writing whole logic to find access
token for org 2.
- Create a custom button "Refresh Access Token" in External Application object.
- Create a VF page “WebServerAuthentication” and apex class “WebServerAuthenticationController”.
- Add custom button "Refresh Access Token" to External Application page layout.
- Create Remote Site Settings records. Specify domain name of org 2 for which you want to fetch access token.
- Now create a External Application records. Specify consumer key (generated while creatin connected app in org 2) in client id field. Enter consumer secret and callback URL as present in connect app record in org 2.
Now we ready to generate access token which for org 2. Go to detail page of record which you created. I have created record with name as "SunilKumar04". Click on Refresh Access Token.
System will redirect you to salesforce login page. Enter the credential of org 2 for which you want access token. After logging, if system ask any permission then click on Allow button. After that you will be redirected to org 1 and you can see the response details on External Application record detail page.
Notes:
- If you are integrating 2 developer org, then create domain in your developer org and use domain URL as endpoint URL in Httprequest.
- You can connect to different org. Create different records in External Application object for different org.
- For more detailed information on obtaing access token, please refere below URL https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com
No comments:
Post a Comment