Frage

I'm trying to access the Pingdom API in Google Apps script following that example: https://developers.google.com/apps-script/external_apis

query = 'credits';
var username = 'foo';
var password = 'bar';
var credentials = username+':'+password;
var url = 'https://'+credentials+'@api.pingdom.com/api/2.0/'+encodeURIComponent(query);
var headers = {
  "App-Key": "abcd",
};
var options = {
    "method": "get",
    "headers": headers,
    'validateHttpsCertificates':false
};
Logger.log(url);
var response = UrlFetchApp.fetch(url);

The code execution breaks with the below error:

Unexpected error: https://foo:bar@api.pingdom.com/api/2.0/credits (line 17, file "Code") Dismiss

If I copy/paste the above URL into a browser it works (i.e. I'm getting a "Missing application key" from the pingdom API which confirms username and password where properly provided, otherwise you get an Invalid Credentials error). I tried with and without encodeURIComponent on credentials and I get the same error. 'muteHttpExceptions':true doesn't help either.

Any idea what could cause the error?

War es hilfreich?

Lösung

It appears that including login information in the URL is causing an error in UrlFetchApp. Please file a bug in our Issue Tracker. In the mean time, put the login information in the Authorization header and it should work correctly.

var response = UrlFetchApp.fetch(url, {
  headers: {
    'Authorization': 'Basic ' + Utilities.base64Encode(username + ':' + password)
  }
});

Andere Tipps

I encountered similar problems trying to access the Gitlab API in Google Apps Script. As mentioned in the accepted answer, including login information in the URL causes an error. The Basic Authorization method in the accepted answer did not work for me in Google Apps Script either. Modifying the authorization header to the 'Bearer' type did the trick.

var url = "https://gitlab.com/api/v4/projects/" + projectId;
var options = {
  'headers' : {Authorization: 'Bearer ' + token}
};
var response = UrlFetchApp.fetch(url, options);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top