无需匿名访问即可从另一个Web应用程序从Google Apps脚本Web应用程序获取JSON [英] Get JSON from a Google Apps Script web-app from another web-app without anonymous access

查看:72
本文介绍了无需匿名访问即可从另一个Web应用程序从Google Apps脚本Web应用程序获取JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这个问题是的重复项;>从另一个Google WebApp的Google WebApp中获取JSON对象,但是,这个问题已有6年历史了,在我的情况下,答案不起作用-以下详细说明.

First, this question is a duplicate of Fetch JSON object from Google WebApp from another Google WebApp however, that question is 6 years old, and the answer does not work in my situation -- details below.

-

我有一个Google Apps脚本网络应用,该应用返回JSON,并且已发布,因此可以像我一样运行,并且组织中的任何人都可以访问它.由于各种原因,我无法以 answered的形式发布为匿名来自另一个问题.

I have a Google Apps Script web-app that returns JSON and is published such that it runs as me and anyone in my organization can access it. For various reasons, I cannot publish it as anonymous as answered from the other question.

function doGet(request)
{
    return ContentService.createTextOutput(JSON.stringify(request)).setMimeType(ContentService.MimeType.JSON);
}

我正在尝试使用UrlFetchApp.fetch(...)从另一个Web应用程序调用此Web应用程序,但是它似乎返回了一堆HTML而不是预期的JSON输出.

I am trying to call this web-app from another web-app using UrlFetchApp.fetch(...) but it seems to return a bunch of HTML instead of the expected JSON output.

function testIt()
{
    var a = UrlFetchApp.fetch("https://script.google.com/a/verizon.com/macros/s/[redacted]/exec");
    Logger.log(a.getContentText());
}

我了解第二个网络应用程序,即具有testIt的网络应用程序,是从Google的服务器运行的,因此UrlFetchApp.fetch调用是匿名的.

I understand that the second web-app, the one with testIt runs from Google's servers so the UrlFetchApp.fetch call is anonymous.

必须有一种方法来传递运行testIt的用户的身份验证令牌?

There must be a way to pass the authentication token of the user running testIt?

我尝试过此方法,但它也不起作用:

I tried this, but it did not work either:

var a = UrlFetchApp.fetch("https://script.google.com/a/verizon.com/macros/s/AKfycbxseotobMLXnid5PT_UpBRWZdNrhhX2EOegeCd4b9gFA2VbAvLm/exec", {
    "method":"GET",
    "muteHttpExceptions": true,
    "headers": {
        "Authorization" : "Basic " + ScriptApp.getOAuthToken()
    }
});

我在做错什么吗?还是我在做什么?

Am I doing something wrong or is what I am after not possible?

基本上,我有一个自己的网络应用程序,而另一个人则拥有自己的网络应用程序.我正在尝试找到一种方法,使他们可以从其Web应用程序调用我的Web应用程序,以执行其Web应用程序无法访问的某些操作(将某些数据写入某些工作表并从我的帐户发送一些电子邮件). /p>

Basically, I have a web-app I own and someone else has a web-app they own. I am trying to find a way they can call my web-app from their web-app to do certain things that their web-app does not have access to (writing some data to some sheets and sending some emails from my account).

推荐答案

当它使用访问令牌访问Web应用程序时,请进行以下修改.

When it accesses to the Web Apps using the access token, please modify as follows.

"Authorization" : "Basic " + ScriptApp.getOAuthToken()

To

"Authorization" : "Bearer " + ScriptApp.getOAuthToken()

  • 作为这种情况的重点,请确认您的范围中是否包含Drive API的范围.
  • 如果未包括在内,请在注释行中输入// DriveApp.getFiles().这样,https://www.googleapis.com/auth/drive.readonly包含在范围中.我认为对于Web Apps,可以将其用于访问.
    • As an important point of this case, please confirm whether the scopes of Drive API are included in your scopes.
    • If that is not included, please put // DriveApp.getFiles() as the comment line. By this, https://www.googleapis.com/auth/drive.readonly is included in the scopes. I think that in the case of Web Apps, this can be used for accessing.
    • 这篇关于无需匿名访问即可从另一个Web应用程序从Google Apps脚本Web应用程序获取JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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