1

I am doing callout from one salesforce org to another saleforce org to get access token but i am getting this error {"error":"unsupported_grant_type","error_description":"grant type not supported"}

I tried using my domain url as well, didnt worked

In connected app i have following access Access and manage your data (api) Provide access to your data via the Web (web) Perform requests on your behalf at any time (refresh_token, offline_access) Allow access to your unique identifier (openid)

HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setEndpoint('https://xxx.salesforce.com/services/oauth2/token');
String CLIENT_ID = 'xyz';
String CLIENT_SECRET = 'xyz';
req.setBody('grant_type=code' + '&client_id='+CLIENT_ID + 
        '&client_secret='+CLIENT_SECRET + '&redirect_uri=https://xxx.my.salesforce.com/');
Http http = new Http();
HTTPResponse response = http.send(req);
System.debug('Body ' + response.getBody());
System.debug('Status ' + response.getStatus());
System.debug('Status code ' + response.getStatusCode());

Please help. Thanks in advance.

2

response_type=code corresponds to the authorization grant type (flow) in oAuth. In Salesforce universe this is known as a Web Server flow. All oAuth flows follow a prescribed sequence of steps. This particular flow involves a redirect with an authorization code which you then exchange for an access token (see docs). The snippet of code you posted is not obtaining an authorization token. Instead you're attempting to get the access token as the first step and that won't work in this specific flow.

For Salesforce-to-Salesforce calls with no user "present" (user's identity is not required for authorization), JWT Bearer flow is the recommended option. A Named Credential can issue a JWT out of the box and exchange it for the access token. See the JWT Token Exchange option and related explanation in this answer for more details on how it works.

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:MyJWT/some_path');
req.setMethod('POST');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.