0

我在使用 Google Apps 脚本调用Streak API时遇到问题。我发出的任何 GET 或 POST 请求都在工作,但我无法让单个 PUT 请求工作。

由于匹配的 CURL 请求有效,我认为这是我对 GAS 请求做错了什么?任何帮助,将不胜感激!

例如这个:

  var streakApiKey = "<my-api-key>";
  var streakBoxKey = "agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM";
  var gmailThreadId = "1611ad242bc28086";
  var url = "https://www.streak.com/api/v1/boxes/" + streakBoxKey + "/threads/";

  var payload = {
    "boxKey": streakBoxKey,
    "threadGmailId": gmailThreadId
    };

  var headers = {
     "Accept": "application/json",
     "Content-Type": "application/json",
     "Authorization": "Basic "+ Utilities.base64Encode(streakApiKey + ":")
     };

  var options = {
    "method": "put",
    "headers": headers,
    "contentType" : "application/json",
    "payload": JSON.stringify(payload),
    "muteHttpExceptions" : true,
    };

  var request = UrlFetchApp.getRequest(url, options);
  Logger.log(request);

  var response = UrlFetchApp.fetch(url,options);
  Logger.log(response.getContentText());

回报:

[18-02-04 10:58:30:284 NZDT] {headers={Authorization=Basic <my-encoded-api-key>, Accept=application/json}, method=put, payload={"boxKey":"agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM","threadGmailId":"1611ad242bc28086"}, followRedirects=true, validateHttpsCertificates=true, useIntranet=false, contentType=application/json, url=https://www.streak.com/api/v1/boxes/agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM/threads/}
[18-02-04 10:58:30:311 NZDT] {
  "success": false,
  "error": "Insufficient params for GmailThread. Missing json"
}

但是这个 CURL 有效:

 curl --request PUT \  --url https://www.streak.com/api/v1/boxes/agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM/threads \  --data 'boxKey=agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM&threadGmailId=1611ad242bc28086' -u <my-api-key>:

同样,这失败了:

var streakApiKey = "<my-api-key>";
  var pipelineKey = "agxzfm1haWxmb29nYWVyNwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEghXb3JrZmxvdxiAgICA1uaPCgw";
  var url = "https://www.streak.com/api/v1/pipelines/" + pipelineKey + "/stages";

  var payload = {
    "name": "new Stage from API",
    };

  var headers = {
     "Accept": "application/json",
     "Content-Type": "application/json",
     "Authorization": "Basic "+ Utilities.base64Encode(streakApiKey + ":")
     };

  var options = {
    "method": "put",
    "headers": headers,
    "contentType" : "application/json",
    "payload": JSON.stringify(payload),
    "muteHttpExceptions" : true,
    };

  var request = UrlFetchApp.getRequest(url, options);
  Logger.log(request);

  var response = UrlFetchApp.fetch(url,options);
  Logger.log(response.getContentText());

并返回:

[18-02-04 11:02:01:600 NZDT] {headers={Authorization=Basic <my-encoded-api-key>, Accept=application/json}, method=put, payload={"name":"new Stage from API"}, followRedirects=true, validateHttpsCertificates=true, useIntranet=false, contentType=application/json, url=https://www.streak.com/api/v1/pipelines/agxzfm1haWxmb29nYWVyNwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEghXb3JrZmxvdxiAgICA1uaPCgw/stages}
[18-02-04 11:02:01:632 NZDT] {
  "success": false,
  "error": "Insufficient params for Stage"
}
4

1 回答 1

0

这个改装怎么样?

修改点:

  • 从您的 curl 示例中,数据似乎是作为“表单数据”发送的。
  • 在您的 GAS 示例中,数据似乎是作为“数据”发送的。
  • 当“Content-Type”为“application/json”时,数据作为“data”发送。
  • JSON.stringify()用于没有“application/json”的“payload”时,“form-data”作为字符串发送。

反映以上几点的修改脚本如下。

对于样品 1:

var streakApiKey = "<my-api-key>";
var streakBoxKey = "agxzfm1haWxmb29nYWVyLwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEgRDYXNlGKGs7kkM";
var gmailThreadId = "1611ad242bc28086";
var url = "https://www.streak.com/api/v1/boxes/" + streakBoxKey + "/threads/";
var payload = {
  "boxKey": streakBoxKey,
  "threadGmailId": gmailThreadId
};
var headers = {
//  "Accept": "application/json", // Modified (I couldn't confirm whether this is required.)
//  "Content-Type": "application/json", // Modified
  "Authorization": "Basic "+ Utilities.base64Encode(streakApiKey + ":")
};
var options = {
  "method": "put",
  "headers": headers,
//  "contentType" : "application/json", // Modified
  "payload": payload, // Modified
  "muteHttpExceptions" : true,
};
var request = UrlFetchApp.getRequest(url, options);
Logger.log(request);
var response = UrlFetchApp.fetch(url,options);
Logger.log(response.getContentText());

对于样本 2:

var streakApiKey = "<my-api-key>";
var pipelineKey = "agxzfm1haWxmb29nYWVyNwsSDE9yZ2FuaXphdGlvbiIQd2hlZWx3b3Jrcy5jby5uegwLEghXb3JrZmxvdxiAgICA1uaPCgw";
var url = "https://www.streak.com/api/v1/pipelines/" + pipelineKey + "/stages";
var payload = {
  "name": "new Stage from API",
};
var headers = {
//  "Accept": "application/json", // Modified (I couldn't confirm whether this is required.)
//  "Content-Type": "application/json", // Modified
  "Authorization": "Basic "+ Utilities.base64Encode(streakApiKey + ":")
};
var options = {
  "method": "put",
  "headers": headers,
//  "contentType" : "application/json", // Modified
  "payload": payload, // Modified
  "muteHttpExceptions" : true,
};
var request = UrlFetchApp.getRequest(url, options);
Logger.log(request);
var response = UrlFetchApp.fetch(url,options);
Logger.log(response.getContentText());

笔记 :

  • 我无法测试这些修改后的脚本,因为我没有令牌和密钥。所以请测试一下。

如果这不起作用,我很抱歉。

于 2018-02-03T23:56:20.540 回答