3

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:

enter image description here

Principal details:

enter image description here Named Credentials:

enter image description here

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!

2
  • 1
    Your config looks correct. In your curl attempt, the value of grant_type must be lowercase (client_credentials rather than CLIENT_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)
    – identigral
    Commented Feb 5 at 22:02
  • 1
    Upvoted your question for including all details, including curl repro. Excellent effort for your first question, welcome to SFSE.
    – identigral
    Commented Feb 5 at 22:11

1 Answer 1

1

I found the answer therefore I share it with you. There were two issues in this case. Both were solved on the mulesoft side.

  1. Mulesoft expected "grant_type"="CLIENT_CREDENTIALS" and received "grant_type"="client_credentials". As @identigral mentioned in the answer above, this is Salesforce default and can not be changed.
  2. Mulesoft expected Content-Type: application/json for token request. Salesforce sends token request with Content-Type: application/x-www-form-urlencoded. This is Salesforce default and can not be changed.

Mulesoft developer aligned connection structure on his end. The alignment helped and the connection is now successfully working!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .