4

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 -

enter image description here

Then created Named credentials of OAuth 2.0 type and authenticated with required scopes mentioned in the Xero developer document.

enter image description here

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

5
  • can you try checking the 'Allow merge fields in HTTP Header' and 'Allow merge fields in HTTP Body'
    – ManishG
    Dec 10, 2019 at 14:27
  • Yes tried the same but the response is same.
    – Arabinda
    Dec 10, 2019 at 14:34
  • Hi, I am facing error after authorizing from xero when it comes back to salesforce it says, We can’t log you in because of an authentication error. For help, contact your Salesforce administrator. on Ui and in URL r?ErrorCode=No_Oauth_Token&ErrorDescription=invalid_client&ProviderId=0SO2v000000Gvc7 Feb 13, 2020 at 4:17
  • @Arabinda can you help. Feb 13, 2020 at 4:18
  • @Shanker Paudel , can you please send some more details about your SF and Xero connection.
    – Arabinda
    Feb 14, 2020 at 12:28

1 Answer 1

4

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.

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:XeroDev/api.xro/2.0/Accounts');
req.setMethod('GET');
/* tenant id is different for different organization */
req.setHeader('xero-tenant-id', 'd5ae8ed7-be06-43c0-a3ee-39e40e4b1eff');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());

If you want you can dynamically fetch xero-tenant-id

GET https://api.xero.com/connections
Authorization Bearer {{access_token}}

response

[
    {
        "id": "f192b8df-f13c-4c12-8b01-d3f7590e98fe",
        "tenantId": "d5ae8ed4-be07-43c0-a3ee-31e40e4b1dff",
        "tenantType": "ORGANISATION",
        "createdDateUtc": "2019-12-11T17:08:13.7204600",
        "updatedDateUtc": "2019-12-12T11:19:59.1985240"
    }
]
4
  • thanks for your help. It worked as expected.
    – Arabinda
    Dec 13, 2019 at 6:12
  • @TariqueH If the access token expired do we need to manually re-authenticate the name credentials? or do we have any automation process to avoid the authentication fail issue ?
    – Satheesh
    May 13, 2021 at 7:24
  • @Satheesh named credential should take care of refreshing expired tokens. At code level you don't have to worry.
    – TariqueH
    Jun 9, 2021 at 11:57
  • Hi All -I followed above approach, but I am getting invalid redirect uri error when saving named credentials. Can someone please help me ? Sep 11, 2022 at 18:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .