通过服务帐户访问作为 Web 应用程序发布的 GSheet 的正确设置

2024-01-11

我有一个作为网络应用程序发布的 GSheet,我试图允许从服务帐户访问它

  • 它被设置为允许任何人访问。
  • 当我点击时我可以访问它 直接部署应用程序 URL。
  • 我已将项目转换为 GCP 标准项目。
  • 我创建了一个附加到的服务帐户 项目。
  • 服务帐户具有编辑者访问权限。
  • 然后我还授予了访问权限 将工作表发送到服务帐户。

尝试使用以下 python 代码访问它并得到 401。 以下示例here https://google-auth.readthedocs.io/en/latest/user-guide.html

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()

我想我错过了一些明显的东西。 有任何想法吗?


  • 您想要访问部署为的 Web 应用程序Execute the app as: Me and Who has access to the app: Anyone using request蟒蛇。
  • 部署 Web Apps 的 Google Apps 脚本项目已与服务帐户共享。

如果我的理解是正确的,那么这个修改怎么样?在这种情况下,请按如下方式修改范围。

From:

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

To:

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

and/or

SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
  • 即使您的Web Apps脚本使用Spreadsheet的方法,似乎也需要Drive API的范围。

Note:

  • 在这种情况下,您可以使用以下命令从 Web 应用程序检索返回值:print(response._body)在你的脚本中。

如果这不是您想要的结果,我深表歉意。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过服务帐户访问作为 Web 应用程序发布的 GSheet 的正确设置 的相关文章

随机推荐