[SalesForce] Refresh Token using Salesforce Named Credentials and Auth Provider

I am performing a rest callout to a 3rd party service. Authentication is set up with a named credential (Named Principal, OAuth2) using an Open ID Connect-authentication provider which uses WSO2 identity server.

Initially, callouts from apex code work fine, until the token expires after 24hrs. Then I am consistently receiving below error:

{"message":"unauthorized request, please refresh your access token","statusCode":401,"error":"Unauthorized"}

Named credentials

Auth Provider
The only "workaround" is to start the OAuth Flow again using the "Start Authentication Flow on Save"-setting in the named credential.

Does anybody have an idea what the issue could be? This question is similar to question( Named Credential – Automatic refresh token does not work with WSO2 Identity server) but no response on that question too?

Best Answer

In the Named Credential you should set Scope to refresh_token.

enter image description here

Related Topic