最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

I'm trying to access the Pingdom API in Google Apps script following that example:

query = 'credits';
var username = 'foo';
var password = 'bar';
var credentials = username+':'+password;
var url = 'https://'+credentials+'@api.pingdom/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?

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

query = 'credits';
var username = 'foo';
var password = 'bar';
var credentials = username+':'+password;
var url = 'https://'+credentials+'@api.pingdom./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?

Share Improve this question edited Jun 4, 2013 at 22:26 Eric Koleda 12.7k1 gold badge35 silver badges52 bronze badges asked May 27, 2013 at 12:54 MaxMax 13.3k32 gold badges93 silver badges146 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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

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./api/v4/projects/" + projectId;
var options = {
  'headers' : {Authorization: 'Bearer ' + token}
};
var response = UrlFetchApp.fetch(url, options);

Actually in my case was very simple:

  var response = UrlFetchApp.fetch(exportUrl, options);
  var blob = response.getBlob();

this was the error line:

Exception: Request failed for https://docs.google. returned code 500. Truncated server response: <meta name="viewport" c... (use muteHttpExceptions option to examine full response) cpOnePdfAndRep @ CP 1 PDF AND REP.gs:37

and it was simply because the sheets that the PDF file was exported from were hidden, just in case.

发布评论

评论列表(0)

  1. 暂无评论