I am trying to use the Named credentials for the first time without using the named credentials directly if I call the endpoints with credentials it works perfectly fine which uses OAuth where I get the Bearer/Access token from Microsoft and use them to post the data to the external system like below
@future (callout=true)
public static void createSegmentD365(String subSegmentID) {
String clientId = 'xxxxxxxxxxx';
String clientSecret = 'xxxxxxxxxx';
String tenant_id = 'xxxxxxxxxx';
String resource = 'https://dev.cloudax.dynamics.com';
String reqbody = 'grant_type=client_credentials&client_id='+clientId+'&client_secret='+clientSecret+'&tenant_id='+tenant_id+'&resource='+resource;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://login.microsoftonline.com/abc.org/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
String bearerToken = resp1.access_token;
JSON2Apex js = new JSON2Apex();
js.dataAreaId = 'jax';
js.SegmentCode = subSegmentID;
String jsonBody = json.serialize(js);
Http http1 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setEndpoint('https://dev.cloudax.dynamics.com/data/parentAccounts');
req1.setMethod('POST');
req1.setTimeout(20000);
req1.setHeader('Authorization','Bearer '+bearerToken);
req1.setBody(jsonBody);
req1.setHeader('Content-Type', 'application/json;charset=UTF-8');
HttpResponse res1 = http1.send(req1);
}
I am trying to use the named credentials now. I set the AUTH Provider and Named Credentials and I get the Authenticated status in the Named Credentials like below
So I changed the Apex Callout like
@future (callout=true)
public static void createSegmentD365(String subSegmentID) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:D365Credential/data/parentAccounts');
req.setHeader('Authorization','Bearer {!$D365Credential.access_token}');
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
req.setMethod('POST');
req.setTimeout(20000);
req.setBody(jsonBody);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('Response Body=========' + res.getBody());
In the debug I get the Authentication failed, not sure what I am missing here any help is greatly appreciated
String reqbody = ...
line of code is not equivalent to your auth provider configuration.login.microsoftonline.com
, you have all these parameters in your Postman and Apex snippet. Where are they in the Named Credential?grant_type=client_credentials
. This is undocumented..but similar questions have been answered on this forum that explain that. That aside, some parameters can be appended and this is documented. Whether appending some of these parameters will or will not be useful depends on the target service (Microsoft in this case). Sounds like you're better off with Apex..