Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
920 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...