2017-09-03 66 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'轉換爲''的意見嗎? 儘管解決方案仍在繼續。 –

相關問題