正确的设置,用于通过服务帐户访问作为Web应用发布的GSheet [英] Correct setup for accessing a GSheet published as a webapp via a service account

查看:53
本文介绍了正确的设置,用于通过服务帐户访问作为Web应用发布的GSheet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发布为Webapp的GSheet,我正试图允许从服务帐户访问它

I have a GSheet published as a webapp and I am trying to allow access to it from a service account

  • 设置为允许访问任何人.
  • 当我点击直接部署的应用程序URL.
  • 我已将项目转换为GCP标准项目.
  • 我创建了一个附加到项目.
  • 服务帐户具有编辑者访问权限.
  • 然后,我还授予了该表格到服务帐户.
  • It is set to allow access to Anyone.
  • I can access it when i hit the deployed app URL directly.
  • I have converted the project to be GCP standard project.
  • I created a Service Account attached to the project.
  • Service account has editor access.
  • I then also granted access to the sheet to the service account.

尝试使用以下python代码访问它并获取401.以下示例此处

Trying to access it using the following python code and getting a 401. Following examples here

from __future__ import print_function
from google.oauth2 import service_account
from google.auth.transport.urllib3 import AuthorizedHttp

SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
credentials = service_account.Credentials.from_service_account_file(
'service_account.json', scopes=SCOPES)


def main():
    try:
        authed_http = AuthorizedHttp(credentials)

        response = authed_http.request(
           'GET',   'https://script.google.com/a/<DOMAIN_NAME_REDACTED>/macros/s/AKfycbwv53GaXRCvjQMYizjqI4PsLeNMAcmCtHjRhZE9AlheIp0qE_s/exec')
    print(response)
except BaseException as err_base2:
    print(err_base2)

if __name__ == '__main__':
    main()

我认为我缺少明显的东西.有什么想法吗?

I think I am missing something obvious. Any ideas?

推荐答案

  • 您要访问部署为以以下身份执行该应用程序的Web应用程序:Me 谁有权使用该应用程序:任何人,使用 request .
  • 部署Web Apps的Google Apps脚本项目已与服务帐户共享.
  • 如果我的理解是正确的,那么该修改如何?在这种情况下,请如下修改范围.

    If my understanding is correct, how about this modification? In this case, please modify the scope as follows.

    SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
    

    收件人:

    SCOPES = ['https://www.googleapis.com/auth/drive']
    

    和/或

    SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
    

    • 即使您的Web Apps脚本使用电子表格的方法,似乎Drive API的范围也是必需的.
      • 在这种情况下,您可以使用脚本中的 print(response._body)从Web Apps检索返回的值.
      • In this case, you can retrieve the returned values from Web Apps with print(response._body) in your script.

      如果这不是您想要的结果,我表示歉意.

      If this was not the result you want, I apologize.

      这篇关于正确的设置,用于通过服务帐户访问作为Web应用发布的GSheet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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