我在使用 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"
}
这个修改怎么样?
修改点:
- 从您的 curl 样本来看,数据似乎是作为"表单数据"发送的。
- 在您的 GAS 样本中,数据似乎作为"数据"发送。
- 当"内容类型"为"应用程序/json"时,数据将作为"数据"发送。
- 当
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());
注意:
- 我无法测试这些修改后的脚本,因为我没有令牌和密钥。所以请测试一下。
如果这不起作用,我很抱歉。