0

I'm attempting to integrate with a third party external service, and I can't find the right mechanism to use for authentication.

The third party requires a callout to a REST endpoint with a username and password, and this returns a token that is valid for a specific period of time, in seconds. This token (rather than the username and password) then needs to be sent in the request header for all subsequent requests. The third party doesn't support an OAuth authentication method, and it's just an access token and no refresh token that is provided in the response.

I've created a Named Credential with Password authentication, and I am able to request the initial token. However, any callouts using the Named Credential to any other of their service calls fail as the username and password is still being used.

Is there a way of setting up the Named Credential to store and use the returned token, or will I need to look at taking the returned token and storing it in a Custom Setting, or Custom Metadata element and just using the Named Credential for the authentication callouts?

Thanks,

Rory.

  • How long is the authentication token valid for and where do the calls originate from? I don't believe you can use a Named Credential for the token, so using a custom setting or storing the token as a cookie are options. – nbrown Jun 5 at 12:20
0

Yes, you need to store that token elsewhere. Platform Cache might be a good use case here, since the token eventually expires, or Custom Settings/Custom Metadata may be appropriate here as well.

You can still use the Named Credentials for both the authorization and API calls, though. To fix the Named Credential problem of always using the username/password, uncheck "Generate Authorization Header" and enable "Use Merge Fields In Headers" instead. Then, when you need to get a token, you use the appropriate header:

req.setHeader('Authorization','{!$Credential.AuthorizationHeaderValue}');

This will plug in the base-64 encoded username/password stored in the Named Credentials on-demand.

| improve this answer | |
  • Thanks sfdcfox - hadn't thought about Platform Cache for storage - makes a lot of sense. – Rory Webber Jun 5 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.