Search code examples
google-apps-scripthttp-status-code-401xero-api

Xero API error [ TokenInvalidAudience ] when fetching tenants list


I have recently started working on Xero API using google apps script (javascript like).

From the ouath2 flow mentioned at https://developer.xero.com/documentation/oauth2/auth-flow, I have been able to get through the authentication mechanism to get access token as well as refreshing the access token.

As for the further use of API, it requires fetching tenant-id. Now, when I try to get the data via the endpoint: https://api.xero.com/connections

it returns an error code 401 with following details:

{"Type":null,"Title":"Unauthorized","Status":401,"Detail":"TokenInvalidAudience: 6CBC9B1478974A5CBF7229AB1D32....","Instance":"a2e2c373-0c73-4ecb-85cf-1b21f828...","Extensions":{}}

Can anyone please guide as to what could be the issue? and how to resolve?

Thanks.

For easy reference, i am using the following code:

function myFunction() 
{
  var url = "https://api.xero.com/connections";
  var accToken = fetchKey(ACCESS_TOKEN_KEY);
  var hdr = {
    "Authorization" : "Bearer " + accToken
};
  var params = {
    headers : hdr,
    method : "get",
    muteHttpExceptions : true
  };
  
  var resp = UrlFetchApp.fetch(url, params);
  return resp.getContentText();
}

Solution

  • After configuring another app and restarting from scratch, it seems my own mistake that I assumed {id_token} same as being access token while it is different but I missed it initially in the long token sequence.

    Thank you for your time!