我可以让我一部分执行Google Apps脚本代码,其余部分可以作为访问用户执行吗? [英] Can I have part of Google Apps Script code execute as me while the rest executes as the accessing user?

查看:76
本文介绍了我可以让我一部分执行Google Apps脚本代码,其余部分可以作为访问用户执行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可写入融合表的应用程序脚本网络应用程序,以及一些可定期缓存的电子表格.我不想向同事提供对融合表的编辑访问权限,他们只能去编辑他们认为合适的条目.

I have a apps script web app that writes to a fusion table, as well as a few spreadsheets which it periodically caches. I do not want to provide edit access to the fusion table to colleagues, who could just go and edit entries as they see fit.

当前,对于我组织内的任何人,Web应用程序均以用户身份执行.但是,我宁愿Web应用程序自己执行任何查询,这样我就无需为可能正在访问它的每个个人用户显式提供权限.

Currently the web app executes as the user, for anyone within my organization, which is the intention. However, I'd rather the web app execute any queries as myself, so that I don't need to explicitly provide permissions to each individual user that might be accessing it.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

是可以的.以前,您将使用OAuth2库,将凭据保存到脚本属性存储中,然后使用REST接口访问要使用您自己的凭据访问的服务.现在,您可以使用Execution API来使用内置服务.

Yes this is possible. Previously you would use the OAuth2 library, save your credentials to the script property store, then use the REST interface to the service you want to access with your own credentials. Now you can use the Execution API to use the built in services.

## Setup

  1. 将OAuth2库添加到您的项目中:
    https://github.com/gsuitedevs/apps-script-oauth2

  1. Add the OAuth2 Library to your project:
    https://github.com/gsuitedevs/apps-script-oauth2

根据README.md进行设置,以匹配项目的范围(查看项目属性).确保将setPropertyStore()指向脚本属性.

Set it up according the the README.md, matching the scopes of your project(look at the project properties). Make sure your point setPropertyStore() to the script properties.

验证您的OAuth2服务.

Authenticate your OAuth2 service.

将脚本发布为执行API

Publish your script as an Execution API

向脚本REST接口添加调用.查看令牌方法,确保将其更改为与您自己的令牌方法匹配.

Add a call to your scripts REST interface. Look at the token method, make sure you change it to match your own.

函数Exec​​uteAsMe(functionName,paramsArray){ var url ='https://script.googleapis.com/v1/scripts/'+ScriptApp.getProjectKey()+':run';

function ExecuteAsMe(functionName,paramsArray){ var url = 'https://script.googleapis.com/v1/scripts/'+ScriptApp.getProjectKey()+':run';

var payload = JSON.stringify({"function":functionName,"parameters":paramsArray,"devMode":true});

var payload = JSON.stringify({"function": functionName ,"parameters":paramsArray, "devMode": true});

var params = {方法:"POST", 标头:{Authorization:"Bearer" + getFusionService().getAccessToken()}, 有效负载:有效载荷, contentType:"application/json", MutantHttpExceptions:true}; var results = UrlFetchApp.fetch(url,params); 返回JSON.parse(results).response.result; }

var params={method:"POST", headers:{Authorization: "Bearer "+ getFusionService().getAccessToken()}, payload:payload, contentType:"application/json", muteHttpExceptions:true}; var results = UrlFetchApp.fetch(url, params); return JSON.parse(results).response.result; }

按您的方式执行功能:var results = ExecuteAsMe("searchFusionTable",['searchTerm', 50]);

Execute the function as you: var results = ExecuteAsMe("searchFusionTable",['searchTerm', 50]);

参考:
执行API
https://developers.google.com/apps-script/guides/rest/api?hl = zh_CN

Reference:
Execution API
https://developers.google.com/apps-script/guides/rest/api?hl=en

OAuth2库
https://github.com/gsuitedevs/apps-script-oauth2

OAuth2 Library
https://github.com/gsuitedevs/apps-script-oauth2

这篇关于我可以让我一部分执行Google Apps脚本代码,其余部分可以作为访问用户执行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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