Unexpected error on UrlFetchApp.fetch in Google Apps script

眉间皱痕 提交于 2019-11-28 11:52:53

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/16773678/unexpected-error-on-urlfetchapp-fetch-in-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!