oauth in salesforce

Steps For Making OAuth Access Setting Through Connected App

1 Add the PostMan Extension to the Browser.
2 Login into ur Oauth Account.
3 Then We have create a Connected App in the Organisation.
4 for that Connected App we will get
● Consumer Key
● Consumer Secret
5 Now we have to set the Callback URL that we will get from Postman (GET NEW ACCESS TOKEN) by callback URL
6 Now in the Postman go to the Authorization and form Type select OAuth 2.0
7 From Existing Token select GET NEW ACCESS TOKEN
8 Now Set
● Auth URl as https://login.salesforce.com/services/oauth2/authorize (For Production).
● Access Token URL https://login.salesforce.com/services/oauth2/token (For Production).
● Client ID will be the Consumer Key of the Connected App.
● Client Secret will be the Consumer Secret of the Connected App.
● Grant Type select Authorization Code.
● Check request access token locally.
● Click Request Token
9 We will get the Token
10 From the token Name use the Instance Url. The URL be like
https://ap5.salesforce.com
Add services/apexrest/jss3
https://ap5.salesforce.com/services/apexrest/jss3 (Final URL)

URL For The Public Access

https://test11-developer-edition.ap5.force.com/naman/services/apexrest/jss1
https://sitename/user/services/apexrest/urlMappingName

Step to call the data in Postman from different org

1 we have to make a post request from Postman on the URL like

https://login.salesforce.com/services/oauth2/token?client_id=3MVG9d8..z.hDcPIbBth31_BjrDbxB2BCMOY4HACQNhfilW3chLMvL5OuAqwzDF1kQG87bd818OJ089scIi51&client_secret=115937524371799465&username=naman.jain6512@gmail.com&password=Naman$1995VcXJ1N6faoQwWWehhGMgRMGQ2&grant_type=password

Url Contain
Username of that user from which ur bring the data.
Password with the Secret Token.
Client_secret of that user from which ur bring the data.
Client_id of that user from which ur bring the data.
Access Token URL of that user from which ur bring the data.

2 This will give us the data we need to access.
3 Now we have to hit it again with the get method of the different URL which is like

Here is the authentication class code :
Authentication Class

public class Aouth {
private static String clientId = '3MVG9YDQS5WtC11rq3DE08phrqNyW1WlIbzvwb7iBmB7RK4MYHagRiDY5BLWYJQ8vyHAP..TX25FtbXz7y0wX';
private static String clientSecret = '1816243741140611103';
private static String username = 'makk@sfdc.com';
private static String password = 'Manku@2000x9mtmHfTqEDLSjtoXhJHff8fl';
public class deserializeResponse
{
public String id;
public String access_token;
}
public static void ReturnAccessToken ()
{
String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);

system.debug(resp1.access_token);

if(resp1.access_token != null){
String endPoint = 'https://dev-makk-dev-ed.my.salesforce.com/services/apexrest/AccountRecord?_HttpMethod=PATCH';

Http h2 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + resp1.access_token);
req1.setHeader('Content-Type','application/json');
// req1.setHeader('accept','application/json');

//req1.setHeader('X-HTTP-Method-Override','PATCH');
req1.setMethod('POST');
req1.setEndpoint(endPoint);
req1.setBody('{"Id":"0016F00002D9TnP","Name":"Kapil Dev","Phone":"9897989712","Description":"He is a good Cricketer"}');
HttpResponse res1 = h2.send(req1);

system.debug(res1.getBody());
}
}
}

Hits: 452

Share Post

By Naman Jain

My Name is Himanshu Rana, 23 Years young, born and grow up in Ghaziabad, India. A High Spirited Salesforce Admin, Developer and a Blogger. I currently work at Wakencode Technologies,

Leave a Reply

Your email address will not be published. Required fields are marked *