我正在尝试连接到 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_TOKEN
、CLIENT_ID
和CLIENT_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_TOKEN
、CLIENT_ID
和CLIENT_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/