且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以让我一部分执行Google Apps脚本代码,其余部分可以作为访问用户执行吗?

更新时间:2023-09-26 12:07:04

是可以的.以前,您将使用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