9

If I am not mistaken, as per UrlFetch Documentation I should be able to call the New Basecamp API from within Google Apps Script. I suspect my confusion comes from a formatting GAS mistake (and the fact that I am just beginning all this). I have googled this a long while. This is what I have and the error I get back from GAS:

myCode in GAS:

function myFunction() {
  var url = "https://basecamp.com/******/api/v1/projects.json";
  var headers = {
                 "contentType": "application/json",
                 "headers":{ "User-Agent": "MY_APP_NAME",
                            "username:password" : "user:pass"},
                 "validateHttpsCertificates" :false
                 };

var response = UrlFetchApp.fetch(url, headers);
var text = response.getResponseCode();
Logger.log(text);
}

GAS Error Message:

Request failed for https://basecamp.com/2166446/api/v1/projects.json returned code 401.
Server response: HTTP Basic: Access denied. (line 9, file "Code")

I hope this is a reasonable question and thank you for the help.

Matteo Stohlman
  • 192
  • 2
  • 13

1 Answers1

16

The correct header for HTTP Basic Authentication, which makes use of Utilities.base64Encode, is as follows:

"headers": {
    "User-Agent": "MY_APP_NAME (App URL/your email address)",
    "Authorization": "Basic " + Utilities.base64Encode(username + ":" + password)
},
  • 1
    @mat10112: The information is all over the place. I started with the link to the API in your original posted, where it's mentioned that you can use HTTP basic authentication. Since that's easier to use than OAuth, I went to Wikipedia to double check the exact header to use for HTTP basic authentication. I then checked out the [App Script API](https://developers.google.com/apps-script/) documentation page, as I knew they had some build-in utilities. Then I just put it together. –  Mar 18 '13 at 22:11
  • Ok, I will search better from now on. I'm new so its hard to navigate when things aren't so familiar. Thanks again for the help. I would upvote if I could. – Matteo Stohlman Mar 18 '13 at 22:13