4

I am trying to make a webservice call to an authorization server (OAuth 2.0) using the "client_credentials" grant type, but i receive a "System.CalloutException: Unable to fetch the OAuth token." when executing this code :

    Http httpCls = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('callout:FasstAPIGEE');
    request.setMethod('POST'); 

When i check the logs , i find these lines, both the name and the Id of the named credential are NULL:

enter image description here

However, when i make the same call via Postaman or curl , it works and i am able to get the access_token

I am sharing with you ma configuration of the named and the external credentials

enter image description here

enter image description here

enter image description here

here is a screenshot of the test i performed in postman

enter image description here

And here the curl command i am using that also works

curl -X POST -u "a1***_x**:xNMq******Gl" -d "grant_type=client_credentials" https://auth.***/oidc/token

Any ideas how to resolve the issue ?

4
  • 1
    does running user have CRUD Read on UserExternalCredential SObject?
    – cropredy
    Commented Mar 11 at 16:15
  • 1
    @cropredy Would be a different error message if it couldn't read the creds.
    – identigral
    Commented Mar 12 at 8:45
  • @cropredy , yes, the running user already have the "Read" right on the UserExternalCredential object
    – user28829
    Commented Mar 12 at 9:28
  • 1
    Did you find what was the issue, I am having exact same issue , I think my server is excepting client info in body.
    – preeti
    Commented Jun 13 at 17:03

2 Answers 2

0

I ran into a similar problem. Turns out that the identity provider url I'm trying to reach for authentication was not setup to follow true OAuth 2.0 standards. It was a custom API and took in the client id and secret as part of a body, not the header, and also required additional unexpected parameters, thus failing when Salesforce tried to call it using its standard processes.

You may need to check to see if this is what's happening to you. It may not be an issue with Salesforce but the identity provider itself. In which case, you will have to go a different route.

-2

HTTP call should not be sent to authorization server, You need to send call to your actual endpoint. Your named credential need to have actual endpoint and external credential need to have your OAuth endpoint. If using client credential flow, Salesforce should auto generate OAuth token(handled automatically) and attach it to your main http call. That’s the benefit of using named credentials/external credentials.

13
  • i am sending my calls to an actual endpoint , i did not share it for the sake of simplicity , but the issue is still the same
    – user28829
    Commented Mar 12 at 9:31
  • One more question - your screenshot only shows external credential, did you create both named and external credential right ? And you are referencing named credential name in your callout. And I had created this article couple of weeks ago, see if this helps - medium.com/@sfdcsushil/…
    – sushilsfdc
    Commented Mar 12 at 11:55
  • Never mind, I see named credentials in related list.
    – sushilsfdc
    Commented Mar 12 at 11:57
  • Only thing I can think of now is trying it without named credentials once to prove that your api is accessible from Salesforce.
    – sushilsfdc
    Commented Mar 12 at 12:05
  • I actually tried to do that to use apex to call the endpoint without named credentials to validate the endpoint, but i think salesforce does not support grant_type=client_credentials from Apex
    – user28829
    Commented Mar 12 at 12:55

You must log in to answer this question.

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