function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhiraj DattaAbhiraj Datta 

In Salesforce is Grant_type=client credentials supported OAuth flow? If so please help me with a sample code showing that or any blog if possible. Thanks!

v varaprasadv varaprasad
Hi Abhiraj,

Please check once below URL : 

http://salesforceprasad.blogspot.com/2017/04/how-to-test-salesforce-rest-api.html

https://sfdcbeginner.com/how-to-test-salesforce-rest-api-using-rest-client.html

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com

 
Daniel Hurran 6Daniel Hurran 6
public string returnAuthToken(String clientId, String clientSecret, String endpoint)
    {
        string accessToken='';
        string username=UserInfo.getUserName();
        string payload = 'client_id=' + clientId + '&client_secret=' + clientSecret + '&username=' + username + '&grant_type=client_credentials&scope=api-full-access';
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint(endpoint + '/connect/token');
        req.setHeader('Content-Type','application/x-www-form-urlencoded');
        req.setHeader('Content-Length',String.valueOf(payload.length()));
        req.setBody(payload);
        Http binding = new Http();
        HttpResponse res = binding.send(req);
        if (res.getStatusCode() == 200) {
            JSONParser parser = JSON.createParser(res.getBody());
            while (parser.nextToken() != null) {
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() =='access_token')){
                    parser.nextToken();
                    accessToken= parser.getText();
                }
            }
        }
        else {
            return null;
        }
        System.debug('accessToken : ' +  accessToken);
        return accessToken;
    }

 
tarique habibullah 20tarique habibullah 20
Hi Daniel,

Not working
new CCGrant().returnAuthToken('$$.$$',
                             '$$$',
                             'https://power-6240-dev-ed.cs77.my.salesforce.com');

getting folowing error
User-added image

Are you sure this will work?
Because I found that we cannot get client credentials grant type in SF.
https://salesforce.stackexchange.com/questions/252922/does-salesforce-support-the-client-credentials-grant-for-oauth
Daniel Hurran 6Daniel Hurran 6
Hi Tariq, my sample was for getting a token from a third party using client_credentials grant type, not Salesforce itself.
tarique habibullah 20tarique habibullah 20
Super. Thanks for the clarification.