I'm using Named Credentials with OAuth authentication. When calling a method on the endpoint, my Apex app receives back the status that the token needs to be refreshed.
How do I tell Salesforce to start the OAuth process for my Named Credential?
Everything is setup and works fine:
- The external SAAS service uses OAuth Authorization Code Grant with its own OAuth endpoints.
- Created Authentication Metadata in SFDC for the service
- Wrote and installed an Auth Provider Plugin Apex class
- Authorized the Remote Site for just the authorization service (not for the API url itself.)
- Added the new Auth Provider
- Added the new Named Credential
- Each user then adds an entry for the Auth Provider via their Personal Settings / Authentication Settings for External Systems screen.
- Completing this step causes the OAuth process to happen: the browser is redirected to the SAAS service's authentication service. After authenticating and authorizing the app, the person is returned to SFDC.
- At this point, the person can use the Apex application which makes use of the Named Credential.
- Life is good.
However, 8 hours later, the token needs to be refreshed.
The best would be for my Apex program to be able to rerun the OAuth flow. The user would then re-login, as needed.
So back to the original question: How do I tell Salesforce to start the OAuth process for my Named Credential or for the user's Authentication Settings for External Systems record?
Currently, if my Apex program detects that the user needs to re-authenticate, I give them the following instructions. What I want is to give the user a button that they push. That would cause the OAuth flow to start.
- From My Settings, enter auth in the Quick Find box, then select Authentication Settings for External Systems
- For the -saas service- entry, click Edit then click Save. Depending on your -saas service- security settings, you may be redirected to the -saas service- to sign in.