1

So basically I'm trying to refresh my access token for access to the xero API on a google sheets spreadsheet. I want to be able to continue using the API without having to re-authorize every 30 minutes. Below is the fetch request I have set up to do this so far. I've tried many different approaches to achieve this but every one is met with an "unsupported_grant_type" error. I'm using this library for managing the xero API. I'm not sure if it is correctly storing the refresh token and how to get the refresh token if it is? Any suggestions on how to get my API to refresh automatically?

function reAuthoriseXero() {
  var driveService = getDriveService();
  var res = UrlFetchApp.fetch('https://identity.xero.com/connect/token', {
    method: 'POST',
    muteHttpExceptions: true,
    headers: {
      ContentType: "application/x-www-form-urlencoded",
      authorization: "Basic " + Utilities.base64Encode(CLIENT_ID + ":" + CLIENT_SECRET),
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
      refresh_token: REFRESH_TOKEN,
      grant_type: 'refresh_token'
    }
  })

  console.log(res.getContentText());
}

  • It doesn't refresh automatically–you have to check for expiration manually, refresh, and then execute the call again. Check out the [`withRetry()`](https://github.com/googleworkspace/apps-script-oauth2/blob/eebdf7884b019dac5160f6554059bf9537fda1cd/samples/Salesforce.gs#L43) method in the Salesforce sample. – Diego Aug 03 '21 at 13:56
  • Hey Patrick, client_id, refresh_token & grant_type should be in the request body, not headers. – RJaus Aug 29 '21 at 23:51

1 Answers1

1

client_id, refresh_token & grant_type should be body params, not headers.

function reAuthoriseXero() {
  var driveService = getDriveService();
  var res = UrlFetchApp.fetch('https://identity.xero.com/connect/token', {
    method: 'POST',
    muteHttpExceptions: true,
    headers: {
      ContentType: "application/x-www-form-urlencoded",
      authorization: "Basic " + Utilities.base64Encode(CLIENT_ID + ":" + CLIENT_SECRET)
    },
    body : "client_id=asdf&refresh_token=asdf&grant_type=refresh_token"
  })

  console.log(res.getContentText());
}
RJaus
  • 176
  • 12