如何通过Google App脚本中的UrlFetch服务调用Google Charts API? [英] How can I call Google Charts API from UrlFetch service in Google App Script?

查看:43
本文介绍了如何通过Google App脚本中的UrlFetch服务调用Google Charts API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一种代理,以便能够使用Google Chart API查询Google Spreadsheet,而不将电子表格的权限直接授予访问可视化效果的人员.

I'm trying to write sort of a proxy to be able to query a Google Spreadsheet with Google Chart API without giving rights directly to the spreadsheet to people accessing the visualization.

为此,我想替换类似的呼叫

In order to do that, I want to replace the calls like

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

通过我的Apps脚本代理(带有JSONP)的调用.

by calls of my Apps Script proxy (with JSONP).

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

I wrote the proxy as following : (I'll add later an option to encapsulate the result in a function - 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":[{原因:" user_not_authenticated,"邮件:"用户未登录," detailed_message:" \ u003ca target = \ u0022_blank \ u0022 href = \ u0022https://spreadsheets.google.com/spreadsheet/\ u0022 \ u003eSignin \ u003c/a \ u003e}]});

When trying to get a response, I obtain this error : 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 ,但是我不知道如何以这种方式对查询进行身份验证.

like if the user was not authenticated. I think it's weird because my script is deployed as a web app to be executed by my user, who has the rights on the spreadsheet. I guess we must use some OAuth, but I don't know how to authenticate the query that way.

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

So my question is : How to authenticate a query with Apps Script to access data with the access rights I defined when deploying as web app ?

推荐答案

请参阅urlfetch文档.您缺少所有oauth资料.您的操作方式仅适用于公共部门.

See the urlfetch docs. You are missing all the oauth stuff. The way you are doing it will only work for a public ss.

这篇关于如何通过Google App脚本中的UrlFetch服务调用Google Charts API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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