【问题标题】:Xero Api in Google Apps Script: How do I refresh access token?Google Apps 脚本中的 Xero Api:如何刷新访问令牌?
【发布时间】:2021-08-03 11:49:23
【问题描述】:

所以基本上我正在尝试刷新我的访问令牌以访问谷歌表格电子表格上的 xero API。我希望能够继续使用 API,而不必每 30 分钟重新授权一次。下面是到目前为止我设置的获取请求。我尝试了许多不同的方法来实现这一点,但每一种方法都会遇到"unsupported_grant_type" 错误。我正在使用this 库来管理 xero API。我不确定它是否正确存储了刷新令牌,如果是,如何获取刷新令牌?有关如何让我的 API 自动刷新的任何建议?

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());
}

【问题讨论】:

  • 它不会自动刷新——你必须手动检查过期,刷新,然后再次执行调用。查看 Salesforce 示例中的 withRetry() 方法。
  • 嗨,Patrick,client_id、refresh_token 和 grant_type 应该在请求正文中,而不是标头中。

标签: google-apps-script oauth-2.0 xero-api


【解决方案1】:

client_id、refresh_token 和 grant_type 应该是正文参数,而不是标题。

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());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 2018-10-31
    • 2015-11-28
    • 2014-07-18
    • 2020-09-13
    • 2019-06-29
    • 2020-09-06
    • 2015-06-19
    相关资源
    最近更新 更多