Google Apps脚本cUrl请求

时间:2016-08-13 01:03:13

标签: curl google-apps-script

我对Google Apps Script很新。我正在尝试创建一个脚本来从公共API中提取数据。

API文档表明可以像这样访问信息:

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>" \
https://api.prosperworks.com/developer_api/v1/leads/2

我在Apps Script中设置了此功能:

function myFunction() {
  var url = "https://api.prosperworks.com/developer_api/v1/leads/2";
  var headers = {
             "contentType": "application/json",
             "headers":{"X-PW-AccessToken": "<TOKEN>",
                        "X-PW-Application": "developer_api",
                        "X-PW-UserEmail": "<USER_EMAIL_ADDRESS>"}
             };

  var response = UrlFetchApp.fetch(url, headers);
  var text = response.getResponseCode();
  Logger.log(text);
}

运行代码时,响应似乎为空,text200

enter image description here

我可以在Postman中拨打电话并获取预期的JSON对象。

Apps Script我做错了什么?如何访问JSON对象?

1 个答案:

答案 0 :(得分:7)

没有代码可以测试,很难确定,但看起来你只需要解析你的JSON

尝试添加:

var data = JSON.parse(response.getContentText());