[SalesForce] Named Credential: Authentication process and refresh token

I've begin working with Named Credential and Authentication Provider for the first time and I'm a bit lost on the authentication process and the refresh token functionalities.

I'm going to add to my managed package this named credential in order for our subscribers to authenticate to their service and allow us to make some callouts to it.

  1. My first question is, can I trigger the authentication flow from another point (e.g: Visualforce page) other than setup? I don't want my subscribers to go to Setup > Security > Named Credentials > Edit > Save in order to start this process.
  2. How can I get the access token and 'update' the Name Credential every N days to renew it before it expires?

Notes:

  • After I ran the authentication flow the name credential displays the following message The authentication provider didn't provide a refresh token. If the access token expires, your org won't be able to access this named credential.
  • I'm integrating with Square and they say I need to renew the access token every 30 days by making a call to POST /oauth2/clients/{client_id}/access-token/renew

Best Answer

Currently, Named Credential refreshes access token only if Refresh token was provided otherwise not. Recently, I got stuck with same issue while developing integration with Instagram and I had to opt the custom solution by using custom setting and Visualforce page.

Actually, Salesforce is following a standard option to refresh the token, they can not provide a customized URL/option to refresh it for each external system....if each system has it's own syntax to refresh token then how Salesforce would handle that for each individual system.