我的设置:
- Python,谷歌应用引擎使用endpoints_proto_datastore
- iOS,端点 Obj-C 客户端库生成器
背景
我已经设置了一个测试谷歌云端点 api 并让它运行得非常快。它工作得很好,在 iOS 模拟器中使用测试应用程序,并使用 Google 的 APIs Explorer。该 API 目前对所有人开放,无需身份验证。
我想:设置一个应用程序可以使用的 API 密钥或系统凭据,以确保它单独可以访问 api - 所有其他人都被拒绝。
Google Endpoints Auth 文档(1) 中的方法是使用Google Developers Console (2)创建 OAuth 2.0 客户端 ID 。因此,我为类型为 iOS 的已安装应用程序创建了一个 ID。到目前为止,一切都很好。
在应用程序中,GTLService 对象看起来像这样......
-(GTLServiceDemogaeapi *)myApiService {
GTMOAuth2Authentication *auth = [[GTMOAuth2Authentication alloc] init];
auth.clientID = @"10???????????????????ie.apps.googleusercontent.com";
auth.clientSecret = @"z????????????3";
auth.scope = @"https://www.googleapis.com/auth/userinfo.email";
static GTLServiceDemogaeapi *service = nil;
if (!service) {
service = [[GTLServiceDemogaeapi alloc] init];
service.authorizer = auth;
service.retryEnabled = YES;
[GTMHTTPFetcher setLoggingEnabled:YES];
}
return service;
}
在 GAE 上,我指定了 (allowed_client_ids 并在方法中添加了用户检查...
@endpoints.api(name='demogaeapi', version='v1',
allowed_client_ids=['10?????????????ie.apps.googleusercontent.com',
endpoints.API_EXPLORER_CLIENT_ID],
scopes=[endpoints.EMAIL_SCOPE],
description='My Demo API')
class MyApi(remote.Service):
@TestModel.method(path='testmodel/{id}', name='testmodel.insert', http_method='POST')
def testModelInsert(self, test_model):
current_user = endpoints.get_current_user()
if current_user is None:
raise endpoints.UnauthorizedException('Invalid token.')
问题
current_user始终为None因此该方法将始终引发异常。看来这是一个已知问题问题 8848:Google Cloud Enpoints get_current_user API 没有填充 user_id (3),而且很快就没有修复的希望。
选项?
- 等到谷歌修复问题 8848。不能真的,我们有一个产品要发布!
编辑:2015 年 5 月 15 日 - 谷歌将问题 8848 设为 WontFix。
我看到可以使用 API 密钥,但是虽然我已经能够创建一个 - 我还没有找到一种在后端启用它的方法。我还注意到这个方法有一个很大的漏洞,谷歌的 APIs Explorer 可以打败它,请参阅 SO question (4)。
此处描述的@endpoints.api 参数: auth_level ( 5) 是否提供了答案?我尝试使用:
@endpoints.api(name='demogaeapi', version='v1', auth_level=endpoints.AUTH_LEVEL.REQUIRED, scopes=[endpoints.EMAIL_SCOPE], description='My Demo API')
但是能够在不使用凭据的情况下从客户端应用程序使用 api。所以它显然没有添加任何身份验证。
向持有共享密钥的客户端查询添加 hiddenProperty。正如bossylobster ( 6)和Carlos (7)所描述的那样。我试过了,但看不到如何获取原始请求对象(另一个问题的主题How to get at the request object when using endpoints_proto_datastore.ndb?)。
@TestModel.method(path='testmodel/{id}', name='testmodel.insert', http_method='POST') def testModelInsert(self, test_model): mykey,keytype = request.get_unrecognized_field_info('hiddenProperty') if mykey != 'my_supersecret_key': raise endpoints.UnauthorizedException('No, you dont!')
编辑:另一个选项浮出水面:
- 使用Google Developers Console (2)创建一个服务帐户。使用此帐户无需用户同意(通过 UI)即可访问 API。但是,Google 似乎将可以通过这种方式添加的应用程序数量限制为 15 或 20 个。请参阅 Google OAuth2 doc (8)。我们很可能会超过限制。
问题
有谁知道我怎样才能让这些选项中的任何一个起作用?或者我应该以不同的方式解决这个问题?
如您所见,我需要指导,帮助,想法...
因为我需要 10 个声誉才能发布 2 个以上的链接:这是我必须提取并添加为参考的链接。确实有点破坏了问题的流程。
- cloud.google.com/appengine/docs/python/endpoints/auth
- console.developers.google.com/
- code.google.com/p/googleappengine/issues/detail?id=8848
- stackoverflow.com/a/26133926/4102061
- cloud.google.com/appengine/docs/python/endpoints/create_api
- stackoverflow.com/a/16803274/4102061
- stackoverflow.com/a/26133926/4102061
- developer.google.com/accounts/docs/OAuth2