google-apps-scriptbasecamp

google apps script post basecamp


I'm trying to make calls to Basecamp's new API through Google Apps Script. GET, I can do. POST, not so much.

Starting with the path

https://basecamp.com/xxxxxxx/api/v1/projects/xxxxxxx/todolists.json

My code:

var headers = {
  'User-Agent' : BCuseragent, 
  'Authorization' : 'Basic ' +  Utilities.base64Encode(BCuser + ':' + BCpass),
  'Content-Type' : 'application/json',
  'Accept' : 'application/json',
  'validateHttpsCertificates' : false
 }

function getBC(path) {
  var url = BCurl + path;
  var opt = {
    'headers' : headers,
    'method' : "GET"
  };

  var response = UrlFetchApp.fetch(url, opt);
  return response;
}


function postBC(path, payload) {
  var url = BCurl + path;

  var opt = {
    'headers' : headers,
    'method' : "POST",
    'payload' : JSON.stringify(payload)
  };
  var response = UrlFetchApp.fetch(url, opt);
  return response;
}

The payload I'm passing as a parameter:

{name: "foo", description: "bar"}

The getBC function works (200 OK), the postBC function returns a 403 error. Yet I am the owner of the project, and I've used curl and a Chrome REST client to confirm I can in fact POST new todolists to this project with the same authorization.

Obviously, my headers are malformed somewhere, but I can't see how.


Solution

  • This is a quirk of UrlFetchApp. You can't set the Content-Type using the general "headers" parameter, instead you must use the "contentType" parameter.

    See the "Advanced Parameters" table here:

    https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object)