Google Apps 脚本 PUT 请求失败

Google Apps Script PUT requests failing

我在使用 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());

Returns:

[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());

和returns:

[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"
}

这个修改怎么样?

修改点:

  • 从您的 curl 样本来看,数据似乎是作为 "form-data".
  • 发送的
  • 在您的 GAS 示例中,数据似乎以 "data".
  • 的形式发送
  • 当"Content-Type"为"application/json"时,数据发送为"data"。
  • JSON.stringify() 用于 "payload" 而没有 "application/json" 时,"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());

注:

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

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