I believe your goal and your current situation as follows.
- You want to use 2 methods of Streak API.
- Create a box
- 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:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…