简体   繁体   中英

google apps script post request to streak api

streak api: https://www.streak.com/api/

I tried to use this streak api with GAS UrlService and found the right syntax for get requests. But I don't find the rigth syntax for put and post:

(1) fe create box

    var RequestArguments = {
      "contentType": "application/json",
      "headers":{
        "User-Agent": "MY_APP_NAME",
        "Authorization": "Basic " + Utilities.base64Encode(streakApiKey),
       },
     "validateHttpsCertificates" :false,
     "method": "PUT",
     "????": "????"
   }; 

  var result = UrlFetchApp.fetch(RequestUrl,RequestArguments);   

(2) fe edit box

    var RequestArguments = {
     "contentType": "application/json",
     "headers":{
      "User-Agent": "MY_APP_NAME",
      "Authorization": "Basic " + Utilities.base64Encode(streakApiKey),
     },
     "validateHttpsCertificates":false,
     "method": "PUT",
     "????": "????"
   }; 

  var result = UrlFetchApp.fetch(RequestUrl,RequestArguments);

I use such way in my Chrome extension, didn't try this code but should be similar in GAS:

  "url": "https://www.streak.com/api/v1/boxes/"+boxKey+"/fields/"+fieldKey;        
  "type": 'POST',
  "dataType": "json",
  "contentType": 'application/json; charset=utf-8',
  "data": {value: yourValue}

It took me some time to figure out the right combination of parameters (Streak API documentation is not friendly in this part), but what I see in your code looks alright to me. It should work.

Here's the function you can use to edit a field in an existing box. Creating a new box will follow the same format.

function editBox(boxKey, fieldKey, value) {
  var url = 'https://www.streak.com/api/v1/boxes/' + boxKey + '/fields/' + fieldKey;
  var params = {
      headers: {Authorization: 'Basic ' + Utilities.base64Encode(STREAK_API_KEY + ":")},
      method: "POST",
      payload: JSON.stringify({
          value: value
      }),
      contentType: 'application/json'
  };
  var field = UrlFetchApp.fetch(url,params );
  return JSON.parse(field.getContentText());
}

This is working:

function editBox() {
  var boxKey =      "xxx";
  var value = "{value:Test}";
  var fieldKey = "1001";
  //{name:"Deal Size", key:"1001", type:"TEXT_INPUT", lastUpdatedTimestamp:1457089319053}

  var url = 'https://www.streak.com/api/v1/boxes/' + boxKey + '/fields/' + fieldKey;

  var params = {
    headers: {Authorization: 'Basic ' + Utilities.base64Encode(streakApiKey)},
    method: "POST",
    payload: value,
    contentType: 'application/json'
  };

  var result = UrlFetchApp.fetch(url,params );

  var string = result.getContentText();
  var Object = JSON.parse(string);    

  return Object;
}


function createBox() {

  var pipelineKey = "xxx";
  var name = "sample box name";

  var url = 'https://www.streak.com/api/v1/pipelines/' + pipelineKey + '/boxes';

  var RequestArguments = {
    headers: {"Authorization": "Basic " + Utilities.base64Encode(streakApiKey)},
    method: "PUT",
    payload: {
      name: name
    }
  };

  var box = UrlFetchApp.fetch(url,RequestArguments);

  var result = JSON.parse(box.getContentText());

  return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM