【问题标题】:Authorize requests to app engine app with a service account使用服务帐号授权对应用引擎应用的请求
【发布时间】:2018-08-29 15:28:56
【问题描述】:

我在handlers 中使用app.yamllogin: admin 来限制对我的应用程序的访问仅限于选定的Google 帐户(我可以在IAM 中进行编辑)。我在 GAE 上使用python27 标准环境。

我想使用我的应用从另一个服务器应用(不在 GAE 上托管)公开的 JSON API。使用服务帐户看起来是一个简单的解决方案,但我无法正确获取范围或请求本身,因此端点会看到经过身份验证的 Google 用户。

service-user 当前在 IAM 中具有 Project/Viewer 角色。我尝试了更多类似AppEngine/ViewerAppEngine/Admin 的方法。我还尝试了更多范围。

我的测试代码:

"""Try do do an API request to a deployed app
with the current service account.

https://google-auth.readthedocs.io/en/latest/user-guide.html
"""
import sys

from google.auth.transport.requests import AuthorizedSession
from google.oauth2 import service_account

def main():
    if len(sys.argv) < 2:
        sys.exit("use: %s url" % sys.argv[0])

    credentials = service_account.Credentials.from_service_account_file(
        'service-user.json')
    scoped_credentials = credentials.with_scopes(
        ['https://www.googleapis.com/auth/cloud-platform.read-only'])
    authed_http = AuthorizedSession(scoped_credentials)

    response = authed_http.request('GET', sys.argv[1])

    print response.status_code, response.reason
    print response.text.encode('utf-8')

if __name__ == '__main__':
    main()

没有错误,请求的行为就像未经身份验证。我检查了服务器上的标头,当从浏览器请求时有几个 session cookie,AuthorizedSession 请求包含单个 Authorization: Bearer .. 标头。

【问题讨论】:

  • 您看到了什么错误?

标签: google-app-engine


【解决方案1】:

通常您需要的角色是App Engine Admin;它是为此目的而设计的。它也应该与viewer/editor/owner primitive roles 一起使用。话虽如此,为确保它不是“角色”问题,只需为其赋予项目所有者角色以及明确的 App Engine Admin 角色,然后重试。这将消除任何基于角色的问题。

让我知道这是否适合你。

【讨论】:

  • 没有错误。服务器认为该请求(完全)未经身份验证。
  • 您是否尝试使用“项目所有者”和“App Engine 管理员”角色?您也可以一步一步检查App Engine Authentication Documentation
猜你喜欢
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多