I'm trying to connect to an external system using legacy Named Credential and a custom Auth. Provider (with AuthProviderPlugin class).
I'm able successfully call the endpoints when the access token is valid. However, when the token expires (the validity is around 18 minutes), I get 200 OK
status but the response body is custom message 401 unauthorized
from the external system.
I believe this causes the issue because Salesforce doesn't re-fetch new token since it is not 401
(https://help.salesforce.com/s/articleView?id=000381864&type=1). Hence, we continue to get this error unless I re-trigger Start Authentication Flow on Save
on the Named Credential.
This is exactly same as this: https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T46qjSAB
Is there a way to force refresh the access token in the named credential when this happens?
- I cannot use the new Named Credential + External Credential because it is not supported by one of the licenses we use. (Salesforce had previously acknowledged this as a product limitation)
- Also, I tried custom logic to fetch access token from Auth. Provider directly using
Auth.AuthToken.getAccessToken()
however, due to some kind of limitation, users get Access Token asnull
unless they hit theOAuth-Only Initialization URL
(Custom REST controller to access Auth.AuthToken.getAccessToken returning null). This is not feasible since we cannot ask all of our SF users to hit this URL.