2017-09-03 98 views
1

我一直在尝试使用外部API来填充Google表单,并且使用澳大利亚API的发货价格。谷歌应用程序脚本UrlFetchApp.fetch请求域(com.au)怪异

发布请求时,应用程序似乎将.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,没有戏剧(它的工作原理)。

谢谢, 杰克

回答

0

​​正在执行该请求。您从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), 
    }); 

我还没有测试过,但我相当有效。

+0

你是传奇。我已经得到了它的工作!下一步是将输出映射到g表中正确的单元格。 –

+0

祝你好运,下一步:)很高兴它有所帮助。 –

+0

@Matteo还有一件事,你有关于接口如何将'.com.au'转换为''的意见吗? 尽管解决方案仍在继续。 –

相关问题