+ Start a Discussion
Yogesh BiyaniYogesh Biyani 

create named credentials

I am using the following code to create an access token. How do I convert this to named credentials? 

TIA
String uuidAPIKeyString = 'somesecret:somekey';
        
        Blob uuidBlob = Blob.valueof(uuidAPIKeyString);
        
        String uuidAuthorization = 'basic ' + EncodingUtil.base64Encode(uuidBlob);
        HttpRequest uuidRequest = new HttpRequest();
        
        uuidRequest.setEndpoint('https://api.somedomain.com/token');
        uuidRequest.setMethod('POST');
        
        uuidRequest.setBody('grant_type=client_credentials');
        uuidRequest.setHeader('Authorization', uuidAuthorization);
        uuidRequest.setHeader('Content-Type', 'application/x-www-form-urlencoded');
        
        Http http = new Http();
        
        HTTPResponse res = new HTTPResponse();
        
        if (Test.isRunningTest() && (mock!=null)) {
            System.debug('Mock Response');
            res = mock.respond(uuidRequest);
            
        } else {
            System.debug('Callout Response');
            res= http.send(uuidRequest);
            
        }
        
        System.debug(res.getBody());     
        
        
        uuidAuthorizationJSON2Apex objuuidAuthJSON2Apex = new 
        uuidAuthorizationJSON2Apex(System.JSON.createParser(res.getBody()));
        System.debug(objuuidAuthJSON2Apex.access_token);
 
        String access_token= objuuidAuthJSON2Apex.access_token;
        system.debug(access_token);
ANUTEJANUTEJ (Salesforce Developers) 
Hi Yogesh,

I found the below article which states on how to set named credentials as endpoints I think you would be giving the username password and endpoint in the named credentials and the created one would be used in the code.

Please have a look at the below link:

>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_named_credentials.htm

I hope this helps and in case if this came in handy can you please choose this as the best answer so that it can be used by others in the future.

Regards,
Anutej
Yogesh BiyaniYogesh Biyani
Hello ANUTEJ, Thanks for the links. I tried the suggestion but kept getting 401 not authorized error message. Is there a way to debug the Named Credential call that Salesforce makes to get more information? Yogesh