首页 > 解决方案 > 如何通过 Apps 脚本在 API 请求中传递会话 ID?

问题描述

我正在尝试通过 Google Sheets / Apps Script访问我们的 CRM文档的 API。通过 Postman 访问 API 时,我没有任何问题,并使用以下设置获得所需的结果:

发布:https://api.sharpspring.com/pubapi/v1.2/?accountID={{accountID}}&secretKey={{secretKey}}

身体:

{
  "id":"12345678912345678999",
  "method": "getOpportunities",
  "params": {
        "where": {},
        "limit":"500",
        "offset": "0"
    }
}

现在,当我尝试在 Apps 脚本中复制相同内容时,我得到以下结果:

{结果:空,错误:{代码:102,消息:'标头缺少请求ID',数据:[]},id:空}

我正在运行的功能如下:

function myFunction() {

  var URL = "https://api.sharpspring.com/pubapi/v1.2/?accountID={{accountID}}&secretKey={{secretKey}}"

  var body = {
    "method": "POST",
    "body": raw,
    "headers": {"Content-Type": "application/json"},
    "redirect": "follow"
  }

  var raw = {
  "method": "getOpportunities",
  "id": "12345678912345678999",
        "params": {
        "where": {},
        "limit":"500",
        "offset": "0"
      }
    }

  var results =  UrlFetchApp.fetch(URL, body).getContentText();
  var data = JSON.parse(results);
  console.log(data);

}

在两者中,我都传递了一个随机会话 ID“12345678912345678999”。我尝试在 cookie 中查找会话 ID,但没有奏效,我认为我在那里走错了路。直接在标头中传递 id 也不起作用。

有任何想法吗?提前非常感谢!

标签: apirestgoogle-apps-scriptsessionidsharpspring

解决方案


推荐阅读