Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

How to update a Streak box via API with google app script?

Whenever a new row is inserted into my spreadsheet I would like to create a box streak. The only solution I found was to create an empty box and then edit it with all the necessary fields. The API call for modifying the box returns me:

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)

This is my code:

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())
}

If I put my parameters in the Streak demo the request returns '200 ok' : https://i.stack.imgur.com/Xv2Zy.png

STREAK API Doc: https://streak.readme.io/reference#edit-a-box

question from:https://stackoverflow.com/questions/65885885/how-to-update-a-streak-box-via-api-with-google-app-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe your goal and your current situation as follows.

  • You want to use 2 methods of Streak API.
    1. Create a box
    2. Update a box
  • You want to achieve this using Google Apps Script.
  • Your {"Authorization": "Basic " + Utilities.base64Encode(STREAK_API_KEY)} can be used for using those methods of Streak API.

Modification points:

  • When I saw the official document you provided, I confirmed the sample scripts of Javascript for Create a box and Update a box as follows.

      // 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"}"
      })
    
  • From above sample scripts, in your script, I thought that createBox() and editBox() are required to be modified.

    • Please set the request body to the property of payload for UrlFetchApp.
    • It seems that "Create a box" is requested with POST method.

When above points are reflected to your script, it becomes as follows.

Modified script:

Before you use this script, please confirm STREAK_API_KEY and the values for each request body, again.

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())
}

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...