I am trying to do the integration between Salesforce and Xero by using Named Credentials (Authentication Protocol is OAuth 2.0).
I created an Auth provider –
Then created Named credentials of OAuth 2.0 type and authenticated with required scopes mentioned in the Xero developer document.
When I am trying to run the below code to get the Account details from Xero, I am getting "AuthenticationUnsuccessful".
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:XeroDev/api.xro/2.0/Accounts');
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
This is the response –
{"title":"Forbidden","status":403,"detail":"AuthenticationUnsuccessful","instance":"707450d8-b455-47ec-a1b4-85bcb2cf80c9"}
Any suggestion on how to make it work by using Named Credential?
Xero URL – https://developer.xero.com/documentation/oauth2/auth-flow
Best Answer
Try adding xero-tenant-id header in you apex code. For this you may need to check "Allow merge fields in HTTP Header" or else it wont allow.
If you want you can dynamically fetch xero-tenant-id
response