【发布时间】:2018-08-29 15:28:56
【问题描述】:
我在handlers 中使用app.yaml 的login: admin 来限制对我的应用程序的访问仅限于选定的Google 帐户(我可以在IAM 中进行编辑)。我在 GAE 上使用python27 标准环境。
我想使用我的应用从另一个服务器应用(不在 GAE 上托管)公开的 JSON API。使用服务帐户看起来是一个简单的解决方案,但我无法正确获取范围或请求本身,因此端点会看到经过身份验证的 Google 用户。
service-user 当前在 IAM 中具有 Project/Viewer 角色。我尝试了更多类似AppEngine/Viewer、AppEngine/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 .. 标头。
【问题讨论】:
-
您看到了什么错误?