Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
939 views
in Technique[技术] by (71.8m points)

javascript - Unexpected error on UrlFetchApp.fetch in Google Apps script

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:[email protected]/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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...