我的设置:
-
Python,谷歌应用引擎使用endpoints_proto_datastore
-
iOS, endpoints Obj-C客户端库生成器
背景
我已经设置了一个测试Google Cloud endpoints api并让它运行得非常快 . 它运行良好,使用iOS模拟器中的测试应用程序,并使用谷歌的API资源管理器 . API目前对所有人开放,无需身份验证 .
我想: setup an API-key or system-credential that can be used by the app to ensure it alone can access the 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.真的不能,我们有一个产品要发布!
EDIT :2015年5月15日 - Google将问题8848发布为WontFix .
-
我看到有可能使用API密钥,但我已经能够创建一个 - 我还没有找到在后端启用它的方法 . 我还注意到这种方法有一个很大的漏洞,谷歌的API资源管理器可以打败它看到问题(4) .
-
这里描述的 @endpoints.api Argument: 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)和卡洛斯(7)所描述的那样 . 我试过这个,但看不到如何得到原始的 request 对象(另一个问题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!')
EDIT: 另一个选项浮出水面:
- 使用Google Developers Console创建服务帐户(2) . 使用此帐户可以访问API,而无需用户同意(通过UI) . 但是,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
-
developers.google.com/accounts/docs/OAuth2
1 回答
一个很好的问题和一个真正的问题 . 我通过拥有自己的"user"数据库表解决了这个问题 .
Long
键(我使用Java)是在写入时自动生成的,这是我用来从那时起唯一标识用户的值 . 写入该表的还有Google ID(通过网页调用时提供),电子邮件地址和iOS ID(在未登录Google帐户时使用iOS应用程序时) .当我的AppEngine endpoints 被调用时,我使用
User
参数中存在的信息(只是登录客户端的经过身份验证的电子邮件地址)或单独的可选iosid
参数,以便从所述表中获取我的内部用户ID号并使用该信息从那时起,唯一地识别我的用户 .我使用相同的表来存储其他特定于用户的信息,所以它与尝试使用
current_user
作为主键没有任何不同,除了我必须将三个字段(googleid,email,iosid)编入索引以进行查找 .