Google Apps脚本中的UrlFetchApp.fetch出现意外错误 [英] Unexpected error on UrlFetchApp.fetch in Google Apps script

查看:146
本文介绍了Google Apps脚本中的UrlFetchApp.fetch出现意外错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问Google Apps脚本中的 Pingdom API 例如:
https://developers.google.com/apps-script/external_apis

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:


意外错误: https:// foo: bar@api.pingdom.com/api/2.0/credits
(第17行,文件代码)解雇

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

如果我将上述URL复制/粘贴到浏览器中,它可以正常工作(即,我从pingdom API中获得一个Missing application key,它确认了正确提供的用户名和密码,否则会出现Invalid Credentials错误) 。我尝试了 encodeURIComponent credentials 上,而且我得到了同样的错误。 'muteHttpExceptions':true 也没有帮助。

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?

推荐答案

似乎在URL中包含登录信息会导致UrlFetchApp中出现错误。请在我们的问题跟踪器中提交错误。同时,将登录信息放在Authorization标头中,它应该可以正常工作。

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

这篇关于Google Apps脚本中的UrlFetchApp.fetch出现意外错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆