I want to configure Named&External Credentials with oAuth client credentials flow and use it in apex. An endpoint is exposed by Mulesoft. I followed those very neat instructions shared here, but I get CalloutException without any error description when I run my code:
apex code:
String inputBodyString = JSON.serialize(inputDetails);
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:e_api/stage/dosomething/withdata');
req.setMethod('POST');
req.setBody(inputBodyString);
req.setTimeout(30000);
HttpResponse response = http.send(req);
Logs:
07:22:26.90 (106240722)|CALLOUT_REQUEST|[31]|System.HttpRequest[Endpoint=callout:e_api/stage/dosomething/withdata, Method=POST]
07:22:26.869 (869720369)|UNKNOWN|NamedCallout[Named Credential Id=null, Named Credential Name=null, Status Code=0, Content-Type=null, Response Size bytes=0, Overall Callout Time ms=0, Connect Time ms=0
07:22:26.90 (870171998)|EXCEPTION_THROWN|[31]|System.CalloutException: Unable to fetch the OAuth token. Error: . Error description: .
Raw callout to obtain the token:
curl --location 'https://api.foobarsitename.com/stage/v1/oauth/token' \
--header 'Content-Type: application/json' \
--data '{
"client_id": "02f63eXXXXXXXXXXXXXXXXXXXXXXX0bc0",
"client_secret": "561XXXXXXXXXXXXXXXXXXXXXXX06029",
"grant_type": "CLIENT_CREDENTIALS"
}'
Raw second callout to get desired info:
curl --location 'https://api.foobarsitename.com/stage/dosomething/withdata' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer q2lGCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXh1BDBw' \
--data '{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
"key5": "value5"
}'
External Credentials:
Principal details:
I already tried to turn off and on all checkboxes with no success. I think all is set up correctly, but still am not sure whether this type of REST API is good to be used with NC/EC. Thank you in advance for any ideas!
grant_type
must be lowercase (client_credentials
rather thanCLIENT_CREDENTIALS
) according to oAuth spec. EC/NC will send in lowercase. If Mulesoft wants uppercase, it could be that...but you'd probably get a more legible error message if this was the case (pun fully intended). Assuming that changing the case doesn't help, open a support case (pun^2, yeah)