尝试运行 UPS OAuth API 时,我不断收到“不支持的授权类型:”错误。它在他们的文档中使用“尝试一下”功能,但在我的实际代码中它不起作用,我基本上复制并粘贴了他们在文档中提供的代码。
这是我的代码:
const formData = {
grant_type: 'client_credentials'
};
const url = 'https://wwwcie.ups.com/security/v1/oauth/token';
const clientID = {clientID};
const clientSecret = {clientSecret};
const auth = 'Basic ' + Utilities.base64Encode(`${clientID}:${clientSecret}`);
let options = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': auth
},
payload: JSON.stringify(formData)
}
这是错误消息
{ [Exception: Request failed for https://wwwcie.ups.com returned code 400. Truncated server response: {"response":{"errors":[{"code":"10400","message":"Unsupported grant type : "}]}} (use muteHttpExceptions option to examine full response)] name: 'Exception' }
谁能帮我弄清楚发生了什么事吗?该文档说“client_credentials”是唯一有效的值,所以我不太确定如何继续。
从你的展示脚本来看,如果你想使用“创建Token”,下面的修改如何?当我看到官方文档时,示例curl命令如下。
curl -i -X POST \
-u <username>:<password> \
https://wwwcie.ups.com/security/v1/oauth/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'x-merchant-id: string' \
-d grant_type=client_credentials
当它转换为 Google Apps 脚本时,它会变成如下所示。
请设置
username
和password
的值。虽然我不确定你的实际情况,但是你的显示脚本中的clientID
和clientSecret
的值分别是username
和password
吗?
function myFunction() {
const username = "username"; // Please set your value.
const password = "password"; // Please set your value.
const url = "https://wwwcie.ups.com/security/v1/oauth/token";
const auth = 'Basic ' + Utilities.base64Encode(`${username}:${password}`);
const options = {
headers: { "Authorization": auth, "x-merchant-id": "string" },
payload: { grant_type: 'client_credentials' }
}
const res = UrlFetchApp.fetch(url, options);
console.log(res.getContentText());
}