繁体   English   中英

使用服务帐户授权对应用引擎应用的请求

[英]Authorize requests to app engine app with a service account

我正在使用app.yamllogin: admin in handlers将访问我的应用仅限于选定的Google帐户(我可以在IAM中编辑)。 我在GAE上使用python27标准环境。

我想使用我的应用程序从另一个服务器应用程序公开的JSON API(不在GAE上托管)。 使用服务帐户看起来像一个简单的解决方案,但我无法正确获取范围或请求,因此端点将看到经过身份验证的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 ..标头。

通常,您需要的角色是App Engine Admin ; 它是为此目的而设计的。 它还应该与viewer / editor / owner原始角色一起使用 话虽这么说,为了确保它不是一个“角色”问题,只需给它项目所有者角色以及显式的App Engine Admin角色,然后再试一次。 这将消除任何基于角色的问题。

如果这对您有用,请告诉我。

暂无
暂无

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

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