简体   繁体   English

通过Google Apps脚本将JSON作为“用户访问网络应用程序”提供

[英]Serving JSON from Google Apps Script as “user accessing the web-app”

Trying to serve a JSON request made by a Google Apps Script as the user who is making the request. 尝试以发出请求的用户身份提供Google Apps脚本发出的JSON请求。 After thinking for a while, I realized that this cannot work, because the serving script needs to be authorized to run as the user who calls him, so the calling script would have to add some authorization info, which it doesn't, and I have no idea how to add that information (that is actually my question: which information to add to the http-request, and from where to get it). 经过一会儿的思考,我意识到这是行不通的,因为需要授权服务脚本以调用他的用户身份运行,所以调用脚本将不得不添加一些授权信息,而事实并非如此,我不知道如何添加该信息(这实际上是我的问题:要向http请求添加哪些信息,以及从何处获取信息)。

But, nevertheless, when I call that server from within the browser, it works because the browser (in which I am logged into my Google account) sends the right info in the http-request to prove that it is authorized. 但是,尽管如此,当我从浏览器中调用该服务器时,它仍能正常工作,因为浏览器(我已登录到我的Google帐户)在http请求中发送了正确的信息以证明它已被授权。

This question is somehow related to the question How to use Google Apps Script ContentService as a REST server , but the answer given there is exactly what I don't want: The invoked JSON-Server should not run under my account, but under the account of the invoking user and use that users resources (scriptdb, drive, ...). 这个问题与如何将Google Apps Script ContentService用作REST服务器有关 ,但是给出的答案正是我所不想要的:所调用的JSON-Server不应在我的帐户下运行,而应在该帐户下运行调用用户并使用该用户的资源(scriptdb,驱动器...)。 So the question is: How to provide that information from one script run from a Google-account to another script, so that this other script can run within that account? 因此,问题是:如何提供从一个脚本到一个Google帐户的信息,以便另一个脚本可以在该帐户中运行? I know that using gas-libraries solves that issue, but I would like to have it as JSON-client/server (Mainly to decouple dependencies). 我知道使用气体库可以解决此问题,但我想将其作为JSON客户端/服务器(主要是为了解除依赖关系)。 To add some code (to keep the Stack Overflow-spirit running), my client: 要添加一些代码(以保持Stack Overflow-spirit的运行),我的客户端:

var scriptUrl="https://script.google.com/macros/s/paperlapapp1231231/exec";
function callIt (nm, args) {
  var a=[nm];
  for (x in args) {
    a.push(args[x]);
  }
  return Utilities.jsonParse(UrlFetchApp.fetch(scriptUrl+"?args="
       +encodeURIComponent(Utilities.jsonStringify(a))).getContentText());
}
function testFunction (p1, p2, p3) { // this is the proxy
  return callIt ("testFunction", arguments);
}
// Testroutine
function callTest() {
  var r=testFunction (9, "XXXlala", {"dudu":1, "dada":"xxx"});
  Logger.log ("r="+Utilities.jsonStringify(r));
}

My server: 我的服务器:

function doGet(request) {
  var ret=null;
  try {
    Logger.log ("I got called with "+JSON.stringify(request));
    var args=JSON.parse(decodeURIComponent (request.parameters.args));
    ret=ContentService.createTextOutput(
            JSON.stringify(this[args.shift()].apply (this, args)))
         .setMimeType(ContentService.MimeType.JSON);
  }
  catch (e) {
    ret=ContentService.createTextOutput(JSON.stringify(e))
          .setMimeType(ContentService.MimeType.JSON);
  }
  return ret;
}
function testFunction (p1, p2, p3) { // this is the implementation
  var s="testing called p1="+p1+", p2="+p2+", p3="+p3;
  Logger.log (s);
  return {testingResult:s};
}

EDIT: 编辑:

Worked out a solution, but requires a Chrome extension and an intermediate proxy server. 制定了解决方案,但需要Chrome扩展程序和中间代理服务器。 See code at https://bitbucket.org/pbhd/gas-rest-server-as-calling-user 参见https://bitbucket.org/pbhd/gas-rest-server-as-calling-user中的代码

Edit: This was not possible before, as my answer below reflects. 编辑:这是不可能的,因为下面我的回答反映了。 However, Google does allow this now, see Execution API https://developers.google.com/apps-script/execution/rest/v1/scripts/run 但是,Google确实允许这样做,请参阅Execution API https://developers.google.com/apps-script/execution/rest/v1/scripts/run


Its not possible to do that from Google Apps Script. 无法通过Google Apps脚本执行此操作。 If I understand it correctly you are calling another Google Apps Script with urlFetch. 如果我理解正确,则说明您正在使用urlFetch调用另一个Google Apps脚本。

Since Google Apps Script doesn't have an oAuth scope you can't make an authenticated call. 由于Google Apps脚本没有oAuth范围,因此无法进行经过身份验证的呼叫。 You can only call a webapp service published as anonymous thus will run always under owner permissions. 您只能调用以匿名方式发布的Webapp服务,因此将始终在所有者许可下运行。

You can't do it with libraries either unless the initial script is also called with user permissions and not owner. 您也不能对库执行此操作,除非还使用用户权限而非所有者调用了初始脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM