我正在尝试将单一的Google App Engine应用程序(使用Python和标准环境)拆分为一个应用程序中的多个服务 . 默认服务是使用另一个服务中的 endpoints 框架调用API .
一切都很好,除了我不明白如何正确检查默认服务的身份验证(并使其在本地开发服务器和 生产环境 中都工作) .
要调用该服务,我正在使用 google-api-python-client
和默认的应用程序凭据 .
from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials
service = build(
name, version,
credentials=GoogleCredentials.get_application_default(),
discoveryServiceUrl=discovery_url)
service.client_token().execute()
我的服务API代码如下所示
@endpoints.api(
name='test',
version='v1',
)
class TestApi(remote.Service):
@endpoints.method(
message_types.VoidMessage,
TestResponse,
path='test',
http_method='GET',
name='test')
def get_test(self, request):
# user = endpoints.get_current_user()
# if not user:
# raise endpoints.UnauthorizedException
return TestResponse(test='test')
在 生产环境 endpoints.get_current_user()
似乎返回一个正确的应用程序用户,但我不是't know how to correctly validate that it' s相同的应用程序 . 在本地开发环境中 endpoints.get_current_user()
返回 None
.
1 回答
你're doing it wrong. You'重新定义
user
,但没有使用它 .以下 example 使用个性化消息和要注销的链接向已登录应用程序的用户表示问候 . 如果用户未登录,该应用会提供指向Google帐户登录页面的链接 .
如果您使用
from google.appengine.api import users
模块:创建
user
时,您仍需要检查它是否为空 . 加user
存储不同的值 . 所以你只需要打电话给他们并定义它们 .如果您有要求用户登录才能访问的页面,则可以在app.yaml文件中强制执行此操作 .
默认情况下,您的应用会使用Google帐户进行身份验证 . 要选择其他选项(例如Google Apps域),请转到Google Cloud Platform控制台中项目的settings页面,然后单击
Edit
. 在Google身份验证dropdown menu
中,选择所需的身份验证类型,然后单击Save
.但是,您也可以使用Tipfy框架 .