amazon-web-services - 谷歌应用程序脚本 : Connecting to Amazon Selling Partner API (Access Token)

标签 amazon-web-services google-apps-script urlfetch

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

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

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

  • grant_type(参数)
  • refresh_token(输入)
  • 范围(参数)
  • client_id(输入)
  • client_secret(输入)

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

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

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

以下是常量变量的格式:

  • REFRESH_TOKEN:Atzr|[a-zA-Z0-9-]*
  • CLIENT_ID:amzn1.application-oa2-client.[a-z0-9]{32}
  • CLIENT_SECRET:[a-z0-9]{64}

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

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

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

更新 #2

我可以在 this 之后在我的 cmd 终端(Windows 用户)上使用以下 curl 命令生成访问 token 示例:

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 的值中是否包含特殊字符。那么,如何反射(reflect) URL 编码?

当以上几点反射(reflect)到你的脚本中时,它变成如下。

修改后的脚本:

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 值是否有效。
  • 顺便说一下,您的问题是为此我们需要以下输入:grant_type(参数)refresh_token(输入)scope(参数)client_id(输入)client_secret(输入)。但是,您的脚本似乎不包含 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)

关于amazon-web-services - 谷歌应用程序脚本 : Connecting to Amazon Selling Partner API (Access Token),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66450176/

相关文章:

google-apps-script - Google 电子表格中未经身份验证的每日限制

python - 为什么要从 Google App Engines 导入 urlfetch?

javascript - 使用基本身份验证的 Google Apps 脚本中的 UrlFetchApp.fetch 出现意外错误

javascript - 气体/Javascript : Save API as a response perform calculations on them and if they meet condition send emails

html - 带有合并单元格的 Google 电子表格表格到 Gmail

javascript - 在 Google Apps 脚本中返回具有合并列的某些行的范围

linux - Amazon EC2 微型实例 - 空间不足?

amazon-web-services - 容器ECS EC2不是以 `running exec setns process for init caused\“exit status 23\”开头:“未知”

security - 我应该对两个 AWS 可用区/区域之间的流量进行 SSL 加密吗?

amazon-web-services - AWS S3 LS --包括 : Unknown options