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
Eric Blaxton 11Eric Blaxton 11 

APEX callout POST for authorization key error

Hi and thanks in advance.

I am trying to get an access key and keep getting the "unsupported grant type" error.

I've verified I can access the url via Postman
User-added image

Apex class:
 
public class ApiBearerKey {
    public static String getBearerKey()
    {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://fakeaddress/token');
        request.setMethod('POST');
        
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setHeader('username','xxx'); 
        request.setHeader('password','xxx'); 
        request.setHeader('grant_type','password');

       
        request.setBody('');
        
        HttpResponse response = http.send(request);
        System.debug('Response : '+response.getBody());
        System.debug('UserInfo : '+UserInfo.getSessionID());
        
        return null;
    }
}
Appreciate the help.

Eric

 
Best Answer chosen by Eric Blaxton 11
Eric Blaxton 11Eric Blaxton 11
Solved it.
public class ApiBearerKey {
    public static String getBearerKey()
    {
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setHeader('Content-Type','application/x-www-form-urlencoded');
        req.setEndpoint('https://fakepath/token');
        req.setBody('grant_type=password' + '&username=fakename' + '&password=fakepwd');
        
       Http h = new Http();
       HTTPResponse res = h.send(req);
       System.debug('Body ' + res.getBody());
       System.debug('Status ' + res.getStatus());
        System.debug('Status code ' + res.getStatusCode());
        
        String strResponse = res.getBody();
        Map<String,Object> newMap = (Map<String, Object>)JSON.deserializeUntyped(strResponse);
        //String tkn = (List<Object>) newMap.get('value');
        string tkn= String.valueOf(newMap.get('access_token'));
        
        
        return tkn;
    }
}


 

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Eric,

Greetings!

Usually the error occurs when the endpoint is incorrect.I would suggest you to double check the Endpoint which is being used 

If it is Sandbox,make sure to use test.salesforce.com or use the valid domain name.

Reference:https://salesforce.stackexchange.com/questions/172980/errorunsupported-grant-type-error-descriptiongrant-type-not-supported

Also,try by logging into the org using the Username and Password to make sure the credentials are valid with the endpoint.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Daniel AhlDaniel Ahl

Hello,
have you tried setting the Content type to x-www-form-urlencoded?
 

request.setHeader('Content-Type', 'application/x-www-form-urlencoded');

​​​​​​​
Eric Blaxton 11Eric Blaxton 11
Hi Daniel,

I tried that to no avail.  I have verified the endpoint in Postman.

Regards,
Eric
Eric Blaxton 11Eric Blaxton 11
Solved it.
public class ApiBearerKey {
    public static String getBearerKey()
    {
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setHeader('Content-Type','application/x-www-form-urlencoded');
        req.setEndpoint('https://fakepath/token');
        req.setBody('grant_type=password' + '&username=fakename' + '&password=fakepwd');
        
       Http h = new Http();
       HTTPResponse res = h.send(req);
       System.debug('Body ' + res.getBody());
       System.debug('Status ' + res.getStatus());
        System.debug('Status code ' + res.getStatusCode());
        
        String strResponse = res.getBody();
        Map<String,Object> newMap = (Map<String, Object>)JSON.deserializeUntyped(strResponse);
        //String tkn = (List<Object>) newMap.get('value');
        string tkn= String.valueOf(newMap.get('access_token'));
        
        
        return tkn;
    }
}


 
This was selected as the best answer