2016-08-14 472 views
1

我對Google Apps Script很新。我正在嘗試創建一個腳本來從公共API中提取數據。我需要傳遞參數,但無法弄清楚如何做到這一點的Apps ScriptApps腳本將參數傳遞給cUrl

的API documenation指示信息可以這樣訪問:

curl -H "Content-Type: application/json" -X GET 
--header "X-PW-AccessToken:<TOKEN>" \ 
--header "X-PW-Application:developer_api" \ 
--header "X-PW-UserEmail:<USER_EMAIL_ADDRESS>" \ 
-d '{"name":"My Lead","email":{"email":"[email protected]","category":"work"}}' 

我有這個功能設置在Apps Script

function myFunction() { 
    var url = "https://api.prosperworks.com/developer_api/v1/leads/search"; 
    var fetchParameters = {}; 
    fetchParameters.method = "post"; 
    fetchParameters.contentType = "application/json"; 
    fetchParameters.headers = { 
     "X-PW-AccessToken": <TOKEN>, 
     "X-PW-Application": "developer_api", 
     "X-PW-UserEmail": <USER_EMAIL_ADDRESS> 
    }; 
    fetchParameters.muteHttpExceptions = true; 
    fetchParameters.payload = {"name":"My Lead","email":{"email":"[email protected]","category":"work"}}; 

    var response = UrlFetchApp.fetch(url, fetchParameters); 
    var dataAll = JSON.parse(response.getContentText()); 
} 

我加fetchParameters.payload,因爲這似乎是如何傳遞的價值觀,但我在解析JSON得到這個錯誤:

Execution failed: SyntaxError: Unexpected token: < 

如果我註釋掉fetchParameters.payload它可以成功運行,但不會影響任何參數(顯然)。

我嘗試過:fetchParameters.d,fetchParameters.data,和其他許多,但似乎沒有任何工作。

回答

1

的API文檔顯示:

-X GET 

您的代碼使用:

fetchParameters.method = "post"; 

嘗試將其更改爲

fetchParameters.method = "get";