我正在嘗試從ShipStation獲取發貨訂單資料到 Google 表格,當我查看Developer tools時,我發現以下 javascript 代碼來獲取訂單串列:
var myHeaders = new Headers();
myHeaders.append("Host", "ssapi.shipstation.com");
myHeaders.append("Authorization", "__YOUR_AUTH_HERE__");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("https://ssapi.shipstation.com/shipments", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
我嘗試獲取的訂單可以在我帳戶的以下鏈接中找到:
https://ship13.shipstation.com/orders/shipped
我嘗試在 Google 應用程式腳本中使用它,但應用程式腳本無法識別new Headers()方法,并且由于我的新手技能,我已經能夠進行以下轉換:
function Test(){
const apiKey = "###"; // Please set your API key.
const url = 'https://ssapi.shipstation.com/shipments';
var options = {
'method': 'GET',
"contentType": 'application/json',
"followRedirects" : false,
'headers': {
'Host': 'ssapi.shipstation.com',
'Authorization': 'apiKey'
}
};
var response = UrlFetchApp.fetch(url,options);
Logger.log(response);
}
但是每次我嘗試運行它時它都會給出以下錯誤訊息:
例外:提供的屬性值無效:標頭:主機
我也API and Secret Key從我的 ShipStation 帳戶中獲得。我發現這個來源可以將 ShipStation 資料輸入 Google 表格,但它僅適用于 MySQL Remoting Service,在這種情況下并沒有真正的幫助。任何指導將不勝感激。謝謝你。
uj5u.com熱心網友回復:
當我從您提供的 URL 中看到示例 curl 命令時,它似乎Host未包含在請求標頭中。而且,UrlFetchApp 不能Host在請求標頭中設定。我認為這可能是您當前問題的原因Exception: Attribute provided with invalid value: Header:Host。那么,下面的修改呢?
修改后的腳本:
function Test() {
const apiKey = "###"; // Please set your API key.
const url = 'https://ssapi.shipstation.com/shipments';
var options = { 'headers': { 'Authorization': apiKey } };
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}
貌似官方檔案中的示例 curl 命令如下。因此,當上述腳本出現錯誤時,請再次確認查詢引數和您的 API 密鑰。
curl -iX GET 'https://ssapi.shipstation.com/shipments?recipientName=recipientName&recipientCountryCode=recipientCountryCode&orderNumber=orderNumber&orderId=orderId&carrierCode=carrierCode&serviceCode=serviceCode&trackingNumber=trackingNumber&createDateStart=createDateStart&createDateEnd=createDateEnd&shipDateStart=shipDateStart&shipDateEnd=shipDateEnd&voidDateStart=voidDateStart&voidDateEnd=voidDateEnd&storeId=storeId&includeShipmentItems=includeShipmentItems&sortBy=sortBy&sortDir=sortDir&page=page&pageSize=pageSize' \ -H 'Authorization: __YOUR_AUTH_HERE__' \
筆記:
如果您
###的const apiKey = "###"不包含 API 機密,那么以下修改如何?function Test() { const apiKey = "###"; // Please set your API key. const apiSecret = "###"; // Please set your API secret. const url = 'https://ssapi.shipstation.com/shipments'; var options = { 'headers': { 'Authorization': 'Basic ' Utilities.base64Encode(`${apiKey}:${apiSecret}`) } }; var response = UrlFetchApp.fetch(url, options); Logger.log(response); }- 當上述腳本出現同樣的錯誤 401 時,請修改
${apiKey}:${apiSecret}為${apiSecret}:${apiKey}.
- 當上述腳本出現同樣的錯誤 401 時,請修改
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/531948.html
標籤:javascriptapi谷歌应用脚本谷歌表格网页抓取
上一篇:CURL回應來自陣列的字串
下一篇:type'(dynamic)=>Category'不是'transform'Flutter型別'(String,dynamic)=>Map
