Google Apps脚本:连接到Amazon Selling Partner API(访问令牌) [英] Google Apps Script: Connecting to Amazon Selling Partner API (Access Token)

查看:79
本文介绍了Google Apps脚本:连接到Amazon Selling Partner API(访问令牌)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到基于文档的第一步是如果当前没有有效令牌,则生成一个访问令牌(访问令牌在生成后一小时内失效).

The first step based on the documentation is to generate an access token if we don't have one valid at the moment (Access tokens expire one hour after they have been generated).

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

For that we would need the following inputs:

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

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

I'm trying to generate an access token for an operation that requires seller authorization

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

Here is my code so far:

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 参数

Note: When we call an operation that requires seller authorization we will set the parameter grant_type = refresh_token and in this case the scope parameter should not be included

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

This is the example code provided by the website:

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个服务器错误:

I'm getting a 500 server error:

例外: https://api.amazon.com 返回的代码请求失败500.截断的服务器响应:{}(使用MutantHttpExceptions选项检查完整响应)AccessToken @ API.gs:30

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的结果为空

Using 'muteHttpExceptions': true only shows that the result of the UrlFetchapp is blank

问题

基于文档 500错误被描述为

Based on the documentation a 500 error is described as

内部服务失败.

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

However I would like to ask if there is some part of my code that I could improve or something else I could check since the response doesn't give me any leads.

更新#1

以下是const变量的格式:

Here are the formats of the const variables:

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

在Amazon提供的示例代码中,他们对此地址执行POST:

In the sample code provided by Amazon they do a POST to this address:

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

但是,我没有在 base_url 上包含 HTTP/l.l .我不确定这是否会导致这个问题.

However I'm not including HTTP/l.l on my base_url. I'm not sure if that would play a role in this problem.

更新#2

我可以在我得到的答复是:

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

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

As Tanaike points out its very likely that Google Apps Script might not be able to request to the endpoint.

推荐答案

修改点:

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

    • The default content type of UrlFetchApp.fetch is application/x-www-form-urlencoded. And, UTF-8 is used.
    • I'm not sure whether the special characters are included in the values of REFRESH_TOKEN, CLIENT_ID and CLIENT_SECRET. So, how about reflecting the URL encode?
    • 当以上几点反映到您的脚本中时,它如下所示.

      When above points are reflected to your script, it becomes as follows.

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

        • Please confirm whether your values of REFRESH_TOKEN, CLIENT_ID and CLIENT_SECRET are the valid values again.
        • By the way, your question says For that we would need the following inputs:grant_type (parameter) refresh_token (input) scope (parameter) client_id (input) client_secret (input). But, it seems that your script doesn't include scope.
        • 根据您更新的问题,当您执行以下curl命令时,

          From your updated question, when your following curl command works fine,

          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命令将数据作为表单数据发送.所以,您可以测试以下示例脚本吗?

          The Google Apps Script is required to be modified. Because your curl command sends the data as the form data. So could you please test the following sample script?

          请设置变量并进行测试.

          Please set the variables and test it.

          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)
          

          这篇关于Google Apps脚本:连接到Amazon Selling Partner API(访问令牌)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆