Google 应用程序脚本 UrlFetchApp.fetch 请求域 (com.au) 奇怪
Google apps script UrlFetchApp.fetch request domain (com.au) oddity
我一直在尝试使用外部 API 来填充 Google Sheet 以及来自澳大利亚 API.
的运费
发布请求时,Appscript 似乎将域的 .com.au
部分更改为 <?>
。
我收到这样的 401 错误(您可以看到 .<?>
):
Request failed for https://www.transdirect.<?>/api/quotes returned code 401. Truncated server response: {"message":"You are not permitted to access this area.","code":401,"success":false} (use muteHttpExceptions option to examine full response) (line 36, file "TransDirect API Handler")
任何人都知道如何在我的请求中阻止这种情况发生?
这是代码(我删除了 API 键。):
function Quote() {
var url= "https://www.transdirect.com.au/api/quotes";
var payload = {'declared_value': '100.00',
'items': [
{
'weight': '38.63',
'height': '0.25',
'width': '1.65',
'length': '3.32',
'quantity': 1,
'description': 'carton'
},
{
'weight': '39.63',
'height': '1.25',
'width': '2.65',
'length': '4.32',
'quantity': 2,
'description': 'carton'
}
],
'sender': {
'postcode': '3',
'suburb': 'SYDNEY',
'type': 'business',
'country': 'AU'
},
'receiver': {
'postcode': '3000',
'suburb': 'MELBOURNE',
'type': 'business',
'country': 'AU'
}};
var response = UrlFetchApp.fetch(
url,
{
'method' : 'post',
'contentType': 'application/json',
'Api-Key': '<API KEY HERE>',
'payload' : JSON.stringify(payload),
}
);
var responseCode = response.getResponseCode()
var responseBody = response.getContentText()
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody)
} else {
Logger.log(Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody)
);
}
};
你会说这是 UrlFetchApp 中的错误还是我做错了什么?
澄清一下,我已经在 apiary.io 上测试了 API-Key,没有任何戏剧效果(有效)。
谢谢,
杰克
UrlFetch
正在执行请求。您从 REST 接口收到错误,因为您的有效负载可能缺少 headers
.
中的 api 键
var response = UrlFetchApp.fetch(url,
{
'method' : 'post',
'contentType': 'application/json',
'headers': { // you need this header field
'Api-key': '<API KEY HERE>', // Api-key with lowcase k
},
'payload' : JSON.stringify(payload),
});
我没有测试过,但我很有信心它会起作用。
我一直在尝试使用外部 API 来填充 Google Sheet 以及来自澳大利亚 API.
的运费发布请求时,Appscript 似乎将域的 .com.au
部分更改为 <?>
。
我收到这样的 401 错误(您可以看到 .<?>
):
Request failed for https://www.transdirect.<?>/api/quotes returned code 401. Truncated server response: {"message":"You are not permitted to access this area.","code":401,"success":false} (use muteHttpExceptions option to examine full response) (line 36, file "TransDirect API Handler")
任何人都知道如何在我的请求中阻止这种情况发生? 这是代码(我删除了 API 键。):
function Quote() {
var url= "https://www.transdirect.com.au/api/quotes";
var payload = {'declared_value': '100.00',
'items': [
{
'weight': '38.63',
'height': '0.25',
'width': '1.65',
'length': '3.32',
'quantity': 1,
'description': 'carton'
},
{
'weight': '39.63',
'height': '1.25',
'width': '2.65',
'length': '4.32',
'quantity': 2,
'description': 'carton'
}
],
'sender': {
'postcode': '3',
'suburb': 'SYDNEY',
'type': 'business',
'country': 'AU'
},
'receiver': {
'postcode': '3000',
'suburb': 'MELBOURNE',
'type': 'business',
'country': 'AU'
}};
var response = UrlFetchApp.fetch(
url,
{
'method' : 'post',
'contentType': 'application/json',
'Api-Key': '<API KEY HERE>',
'payload' : JSON.stringify(payload),
}
);
var responseCode = response.getResponseCode()
var responseBody = response.getContentText()
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody)
} else {
Logger.log(Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody)
);
}
};
你会说这是 UrlFetchApp 中的错误还是我做错了什么?
澄清一下,我已经在 apiary.io 上测试了 API-Key,没有任何戏剧效果(有效)。
谢谢, 杰克
UrlFetch
正在执行请求。您从 REST 接口收到错误,因为您的有效负载可能缺少 headers
.
var response = UrlFetchApp.fetch(url,
{
'method' : 'post',
'contentType': 'application/json',
'headers': { // you need this header field
'Api-key': '<API KEY HERE>', // Api-key with lowcase k
},
'payload' : JSON.stringify(payload),
});
我没有测试过,但我很有信心它会起作用。