11

我的设置:

  1. Python,谷歌应用引擎使用endpoints_proto_datastore
  2. 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),而且很快就没有修复的希望。

选项?

  1. 等到谷歌修复问题 8848。不能真的,我们有一个产品要发布!

编辑:2015 年 5 月 15 日 - 谷歌将问题 8848 设为 WontFix。

  1. 我看到可以使用 API 密钥,但是虽然我已经能够创建一个 - 我还没有找到一种在后端启用它的方法。我还注意到这个方法有一个很大的漏洞,谷歌的 APIs Explorer 可以打败它,请参阅 SO question (4)。

  2. 此处描述@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。所以它显然没有添加任何身份验证。

  3. 向持有共享密钥的客户端查询添加 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!')
    

编辑:另一个选项浮出水面:

  1. 使用Google Developers Console (2)创建一个服务帐户。使用此帐户无需用户同意(通过 UI)即可访问 API。但是,Google 似乎将可以通过这种方式添加的应用程序数量限制为 15 或 20 个。请参阅 Google OAuth2 doc (8)。我们很可能会超过限制。

问题

有谁知道我怎样才能让这些选项中的任何一个起作用?或者我应该以不同的方式解决这个问题?

如您所见,我需要指导,帮助,想法...


因为我需要 10 个声誉才能发布 2 个以上的链接:这是我必须提取并添加为参考的链接。确实有点破坏了问题的流程。

  1. cloud.google.com/appengine/docs/python/endpoints/auth
  2. console.developers.google.com/
  3. code.google.com/p/googleappengine/issues/detail?id=8848
  4. stackoverflow.com/a/26133926/4102061
  5. cloud.google.com/appengine/docs/python/endpoints/create_api
  6. stackoverflow.com/a/16803274/4102061
  7. stackoverflow.com/a/26133926/4102061
  8. developer.google.com/accounts/docs/OAuth2
4

1 回答 1

1

一个很好的问题和一个真正的问题。我通过拥有自己的“用户”数据库表来解决它。密钥(我使用 Java)是在写入时自动生成的Long,这就是我从那时起用来唯一标识用户的值。还写入该表的还有 Google ID(如果通过网页调用则提供)、电子邮件地址和 iOS id(在未登录 Google 帐户的情况下使用 iOS 应用程序时)。

当我的 AppEngine 端点被调用时,我使用参数存在的信息User(只是登录客户端的经过身份验证的电子邮件地址)或单独的可选iosid参数,以便从所述表中获取我的内部用户 ID 号并从那时起使用它以唯一标识我的用户。

我也使用同一个表来存储其他特定于用户的信息,因此它实际上与尝试用current_user作主键没有什么不同,除了我必须为查找创建三个字段(googleid、email、iosid)索引。

于 2015-04-02T12:27:31.567 回答