如何从Google App Script中的UrlFetch服务调用Google Charts API?

时间:2013-05-06 08:02:44

标签: google-apps-script google-visualization

我正在尝试编写一种代理,以便能够使用Google Chart API查询Google电子表格,而无需直接向电子表格授予访问可视化对象的权限。

为了做到这一点,我想替换像

这样的电话
var query = new google.visualization.Query('http://spreadsheets.google.com/tq?key=THE_KEY');
query.send(handleQueryResponse);

通过调用我的Apps脚本代理(使用JSONP)。

我写了代理如下: (我稍后会添加一个选项来将结果封装在函数中 - JSONP)

function doGet(e) {
  return ContentService
    .createTextOutput(
      UrlFetchApp
        .fetch("http://spreadsheets.google.com/tq?tq="+e.parameter.q+"&key="+e.parameter.key))
        .setMimeType(ContentService.MimeType.JSON);
}

尝试获取回复时,我收到此错误:google.visualization.Query.setResponse({"version":"0.6","status":"error","errors":[{"reason":"user_not_authenticated","message":"User not signed in","detailed_message":"\u003ca target=\u0022_blank\u0022 href=\u0022https://spreadsheets.google.com/spreadsheet/\u0022\u003eSign in\u003c/a\u003e"}]});

就像用户未经过身份验证一样。我认为这很奇怪,因为我的脚本部署为一个Web应用程序,由我的用户执行,该用户拥有电子表格的权限。我想我们必须使用一些OAuth,但我不知道如何以这种方式验证查询。

所以我的问题是: 如何使用Apps Script对查询进行身份验证,以使用我在部署为Web应用程序时定义的访问权限来访问数据?

1 个答案:

答案 0 :(得分:0)

请参阅urlfetch文档。你缺少所有的oauth东西。 你这样做只会对公众有用。