Google Apps 脚本:连接到亚马逊销售合作伙伴 API(访问令牌)

Google Apps Script: Connecting to Amazon Selling Partner API (Access Token)

我正在尝试使用 Google Apps 脚本连接到 Amazon Selling Partner API

根据文档的第一步是生成一个 访问令牌 如果我们目前没有有效的访问令牌(访问令牌在生成一小时后过期) ).

为此,我们需要以下输入:

我正在尝试为需要卖家授权的操作生成访问令牌

到目前为止,这是我的代码:

const REFRESH_TOKEN = "MyRefreshToken";
const CLIENT_ID ="MyClientID";
const CLIENT_SECRET = "MyClientSecret"

function AccessToken(){
  var base_url = 'https://api.amazon.com/auth/o2/token';
  var pointParams = "?grant_type=refresh_token&refresh_token=" + REFRESH_TOKEN + "&client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET;
  var query_string = base_url + pointParams;

  var headers = {
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    //'Host': 'api.amazon.com' (ignored since we already provided the url in the base_url var)
  }
  var options = {
    'method' : 'POST',
    'headers': headers,
    //'muteHttpExceptions': true

  };

  var rawData = UrlFetchApp.fetch(query_string, options).getContentText();
  Logger.log('rawData: ' + rawData)
}

注意:当我们调用需要卖家授权的操作时,我们将设置参数grant_type = refresh_token,在这种情况下不应包含scope参数

这是网站提供的示例代码:

POST /auth/o2/token HTTP/l.l
Host: api.amazon.com
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
grant_type=refresh_token
&refresh_token=Aztr|...
&client_id=foodev
&client_secret=Y76SDl2F

问题

我收到 500 服务器错误:

Exception: Request failed for https://api.amazon.com returned code 500. Truncated server response: {} (use muteHttpExceptions option to examine full response) AccessToken @ API.gs:30

使用'muteHttpExceptions': true只显示UrlFetchapp的结果为空

问题

基于 documentation 500 错误描述为

There was an internal service failure.

但是我想问一下我的代码中是否有我可以改进的部分或者我可以检查的其他部分,因为响应没有给我任何线索。

更新 #1

常量变量的格式如下:

在亚马逊提供的示例代码中,他们对这个地址做了一个POST:

POST /auth/o2/token HTTP/l.l

但是我的 base_url 中没有包含 HTTP/l.l。我不确定这是否会在这个问题中发挥作用。

更新 #2

我可以在我的 cmd 终端(Windows 用户)上使用以下 curl 命令生成访问令牌,遵循 this 示例:

curl -k -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=refresh_token&refresh_token=Atzr|IwE....&client_id=amzn1.application-oa2-client.d67...&client_secret=317dcd..." https://api.amazon.com/auth/O2/token

我得到的回复是:

{"access_token":"Atza|IwEB...","token_type":"bearer","expires_in":3600}

正如 Tanaike 指出的那样,Google Apps 脚本很可能无法向端点发出请求。

修改点:

  • UrlFetchApp.fetch 的默认内容类型是 application/x-www-form-urlencoded。并且,使用了 UTF-8。
  • 我不确定 REFRESH_TOKENCLIENT_IDCLIENT_SECRET 的值中是否包含特殊字符。那么,如何反映 URL 编码?

当以上几点反映到你的脚本中,就变成了下面这样。

修改后的脚本:

var base_url = 'https://api.amazon.com/auth/o2/token';
var pointParams = "?grant_type=refresh_token&refresh_token=" + encodeURIComponent(REFRESH_TOKEN) + "&client_id=" + encodeURIComponent(CLIENT_ID) + "&client_secret=" + encodeURIComponent(CLIENT_SECRET);
var query_string = base_url + pointParams;
var options = {
  'method' : 'POST',
  //'muteHttpExceptions': true
};

注:

  • 请再次确认您的 REFRESH_TOKENCLIENT_IDCLIENT_SECRET 值是否有效。
  • 顺便说一句,你的问题是For that we would need the following inputs:grant_type (parameter) refresh_token (input) scope (parameter) client_id (input) client_secret (input)。但是,您的脚本似乎不包含 scope.

已添加:

根据你更新的问题,当你的以下 curl 命令工作正常时,

curl -k -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=refresh_token&refresh_token=Atzr|IwE....&client_id=amzn1.application-oa2-client.d67...&client_secret=317dcd..." https://api.amazon.com/auth/O2/token

需要修改 Google Apps 脚本。因为您的 curl 命令将数据作为表单数据发送。那么,您能否测试以下示例脚本?

示例脚本:

请设置变量并测试。

var base_url = 'https://api.amazon.com/auth/O2/token';
var options = {
  method: "post",
  payload: {
    grant_type: "refresh_token",
    refresh_token: REFRESH_TOKEN,
    client_id: CLIENT_ID,
    client_secret: CLIENT_SECRET
  },
  // muteHttpExceptions: true
};
var rawData = UrlFetchApp.fetch(base_url, options).getContentText();
Logger.log(rawData)

Content-type "application/x-www-form-urlencoded" 无效。将其更改为 "application/json" 并且有效:

function AccessToken() {
  var url = "https://api.amazon.com/auth/o2/token"
  var params= {
    "method":"POST",
    headers: {
      "Content-Type":"application/json"
    },
    payload: JSON.stringify({
      "client_id":CLIENT_ID,
      "client_secret":CLIENT_SECRET,
      "grant_type":"refresh_token",
      "refresh_token":REFRESH_TOKEN
      //"scope":"appstore::apps:readwrite"
    })
  };
  var response = JSON.parse(UrlFetchApp.fetch(url, params).getContentText());
  Logger.log(response)
  return response.access_token
}