首页 > 解决方案 > 如何使用谷歌应用脚​​本通过 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

标签: google-apps-script

解决方案


我相信你的目标和你目前的情况如下。

  • 您想使用 Streak API 的 2 种方法。
    1. 创建一个盒子
    2. 更新一个盒子
  • 您想使用 Google Apps 脚本实现此目的。
  • {"Authorization": "Basic " + Utilities.base64Encode(STREAK_API_KEY)}可以用于使用 Streak API 的那些方法。

修改点:

  • 当我看到你提供的官方文档时,我确认了Create a boxUpdate 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())
}

参考:


推荐阅读