0

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();
}
Fi Teach
  • 88
  • 9
  • Can you share your "fetchKey" function? – Aerials Jul 02 '20 at 14:47
  • In my code, when retrieving the tenant id I set the content-type to "application/json", in contrast to the usual x-www-form-urlencoded. Could that be a difference? Does your scope include accounting.settings? – droopsnoot Jul 02 '20 at 17:33
  • @Aerials I forgot to mention about that but it is only a function wrapper to get the saved access token from properties storage. – Fi Teach Jul 03 '20 at 03:47
  • @droopsnoot Thank you for your suggestions. I have tried with both ideas but the error message is the same. – Fi Teach Jul 03 '20 at 03:48

1 Answers1

0

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!

Fi Teach
  • 88
  • 9