I'm building an integration to a service that is protected by Azure OAuth.
I've set up an registered app that I've been able to use to fetch a valid accesstoken to the Service with the following code:
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
String requestString =
'grant_type=' +
grant_type +
'&client_id=' +
client_id +
'&client_secret=' +
EncodingUtil.urlEncode(client_secret, 'UTF-8') +
'&resource=' +
resource;
The Client id, Client secret and resource is currently stored in a Custom setting which I don't think is ideal, so I was wondering if i maybe could set up the Registered app as an Auth. Provider, and then link that to the named credential so that I
- Don't need to write code to deal with the authentication
- Don't have to keep authentication credentials stored in a custom setting.
I've been able to register the Auth. provider like this:
However, when using the auth provider with the named credential instead of fetching the access token via the custom "authentication" method, i cannot access the service due to the access token provided by the auth. provider being invalid.
My guess is that this is due to the "resource" parameter not being set, and hence the access token provided is not valid for the service i'm trying to access.
Anyone who has faced a similar issue? Is it possible to use an auth. provider to authenticate to a service in this way? Can i provide a resource that i want to access to the Auth. provider?
Thanks for reading!