google-apps-script - 如何使用谷歌应用脚本通过 API 更新 Streak 框?
问题描述
每当在我的电子表格中插入新行时,我都想创建一个盒子条纹。我找到的唯一解决方案是创建一个空框,然后使用所有必要的字段对其进行编辑。修改框的 API 调用返回给我:
Error
Exception: Request failed for https://www.streak.com returned code 400. Truncated server response: {
"success": false,
"error": "Missing parameters"
} (use muteHttpExceptions option to examine full response)
这是我的代码:
function createBox() {
var pipelineKey = "xxxxxx";
var name = "sample box name";
var url = 'https://www.streak.com/api/v1/pipelines/' + pipelineKey + '/boxes';
var RequestArguments = {
headers: {"Authorization": "Basic " + Utilities.base64Encode(STREAK_API_KEY)},
method: "PUT",
payload: {
name: name
}
};
var result = UrlFetchApp.fetch(url,RequestArguments);
var box = JSON.parse(result.getContentText());
return box.boxKey;
}
function editBox() {
var boxKey = createBox();
var response = UrlFetchApp.fetch("https://www.streak.com/api/v1/boxes/"+boxKey, {
"method": "POST",
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic "+ AUTH_KEY
},
"body": "{\"notes\":\"note test\",\"stageKey\":\"5002\",\"name\":\"test from demo\"}"
});
Logger.log(response.getResponseCode())
}
如果我将参数放在 Streak 演示中,请求会返回“200 ok”:https ://i.stack.imgur.com/Xv2Zy.png
STREAK API 文档:https ://streak.readme.io/reference#edit-a-box
解决方案
我相信你的目标和你目前的情况如下。
- 您想使用 Streak API 的 2 种方法。
- 您想使用 Google Apps 脚本实现此目的。
- 您
{"Authorization": "Basic " + Utilities.base64Encode(STREAK_API_KEY)}
可以用于使用 Streak API 的那些方法。
修改点:
当我看到你提供的官方文档时,我确认了Create a box和Update a box的 Javascript 示例脚本如下。
// For Create a box fetch("https://www.streak.com/api/v2/pipelines/pipelineKey/boxes", { "method": "POST", "headers": { "Content-Type": "application/json" }, "body": "{\"name\":\"sampleName\"}" }) // For Update a box fetch("https://www.streak.com/api/v1/boxes/boxKey", { "method": "POST", "headers": { "Content-Type": "application/json" }, "body": "{\"name\":\"sampleName\",\"notes\":\"sampleNotes\",\"stageKey\":\"sampleStageKey\"}" })
从上面的示例脚本中,在您的脚本中,我认为
createBox()
并且editBox()
需要对其进行修改。- 请将请求正文设置
payload
为 UrlFetchApp 的属性。 - 似乎使用 POST 方法请求“创建一个盒子”。
- 请将请求正文设置
当以上几点反映到您的脚本时,它变成如下。
修改后的脚本:
在使用此脚本之前,请STREAK_API_KEY
再次确认每个请求正文的值。
function createBox() {
var pipelineKey = "xxxxxx";
var name = "sample box name";
var url = 'https://www.streak.com/api/v1/pipelines/' + pipelineKey + '/boxes';
var RequestArguments = {
headers: {"Authorization": "Basic " + Utilities.base64Encode(STREAK_API_KEY)},
method: "POST",
payload: JSON.stringify({name: name}),
contentType: "application/json"
};
var result = UrlFetchApp.fetch(url, RequestArguments);
var box = JSON.parse(result.getContentText());
return box.boxKey;
}
function editBox() {
var boxKey = createBox();
var url = "https://www.streak.com/api/v1/boxes/" + boxKey;
var RequestArguments = {
headers: {"Authorization": "Basic " + Utilities.base64Encode(STREAK_API_KEY)},
method: "POST",
payload: JSON.stringify({notes: "note test", stageKey: "5002", name: "test from demo"}),
contentType: "application/json"
};
var result = UrlFetchApp.fetch(url,RequestArguments);
console.log(result.getContentText())
}
参考:
推荐阅读
- groovy - SoapUI:使用属性传输传输 groovy 脚本结果
- php - 异常:Illuminate \ Broadcasting \ BroadcastException PusherBroadcaster.php:119 中没有消息
- gesture - Hololens 模拟手势
- c# - 删除换行符
- javascript - 一本书的几位作者在 SQL
- javascript - 状态更改后组件未重新渲染
- sql - 获取所有具有运行总数的唯一组合
- mysql - 统计相似属性值
- facebook - 通过 facebook api 为页面创建评论
- javascript - 理解带有未捕获承诺的消息