首页 文章

关于Appengine的Oauth与Google Play服务

提问于
浏览
3

我在Android上使用Google Play服务来访问Google Apis和Google Cloud endpoints . 我还可以使用Google Play服务中的令牌访问appengine User api . 这可能吗? OAuth的这个示例代码为link,但有点模糊 . 我可以在 Headers 中传递oauth令牌并获取下面代码的用户吗?

User user = null;
    try {
        OAuthService oauth = OAuthServiceFactory.getOAuthService();
        user = oauth.getCurrentUser();

    } catch (OAuthRequestException e) {
        // The consumer made an invalid OAuth request, used an access token that was
        // revoked, or did not provide OAuth information.
        // ...
    }

1 回答

  • 6

    您可以,但这种方法不会像您所处的场景一样安全:

    • 使用特定于服务的Google API范围

    • 直接访问Endpoints API

    您可以使用Google Play Services obtain a token作为您对使用App Engine上的 Users API感兴趣的范围,您需要 userinfo.email 范围:

    String mScope = "https://www.googleapis.com/auth/userinfo.email";
    try {
        token = GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
    } catch {
        ...
    }
    

    通过Authorization标头将其发送到App Engine:

    Authorization: Bearer your_token
    

    然后,使用 OAuth API,您可以获取 User 对象:

    String mScope = "https://www.googleapis.com/auth/userinfo.email";
    User user = null;
    try {
      OAuthService oauth = OAuthServiceFactory.getOAuthService();
      user = oauth.getCurrentUser(mScope);
    } catch (OAuthRequestException e) {
      // The consumer made an invalid OAuth request, used an access token that was
      // revoked, or did not provide OAuth information.
      // ...
    }
    

    But, in general you don't want to do this! 如果您以这种方式保护您的应用程序,另一个用户可以编写一个应用程序,要求您许可您的 userinfo.email 范围 . 一旦被授予,他们需要做的就是获取令牌并将其传递给您的应用程序,它们就像您一样出现 . endpoints 和其他Google API具有额外的逻辑来防止这种行为,这就是为什么你最好使用这些方法之一 .

相关问题