Android authentication with Google OpenID. What next?

后端 未结 1 742
旧巷少年郎
旧巷少年郎 2020-12-29 11:29

I am not a programmer, but I need to do this myself. I need some help.

I have been looking for the solution for the last two days and I cannot find any.

Ok.

相关标签:
1条回答
  • 2020-12-29 12:27

    Actually, OAuth 2 is what you want, rather than OpenID -- OpenID is inherently web-based, so you'd need to jump through some hoops with WebView or the browser. OAuth 2 allows you to use the token from AccountManager with Google APIs right from the app.

    In your call to getAuthToken(), the authTokenType parameter is the OAuth 2 scope, which you want to be userinfo.profile and userinfo.email to authenticate the email address (you already have it, but you haven't verified it; it could in theory be spoofed) and to get the name of the user.

    Here's what I use for the full scope in a similar situation:

    private static final String OAUTH2_SCOPE =
        "oauth2:" +
        "https://www.googleapis.com/auth/userinfo.profile" +
        " " +
        "https://www.googleapis.com/auth/userinfo.email";
    

    Of course, you could just use the whole string literal inline, but I prefer to build it up and be clear, and it makes it easier to change later if necessary.

    In my case, I use getAuthTokenByFeatures(), something like this:

    am.getAuthTokenByFeatures("com.google", OAUTH2_SCOPE, null, this, null, null,
                              new AccountManagerCallback<Bundle>()
    {
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                Bundle bundle = future.getResult();
                System.out.println("Got Bundle:\n" +
                                   " act name: " +
                                   bundle.getString(AccountManager.KEY_ACCOUNT_NAME) +
                                   "\n act type: " +
                                   bundle.getString(AccountManager.KEY_ACCOUNT_TYPE) +
                                   "\n auth token: " +
                                   bundle.getString(AccountManager.KEY_AUTHTOKEN));
            } catch (Exception e) {
                System.out.println("getAuthTokenByFeatures() cancelled or failed:");
                e.printStackTrace();
            }
        }
    }, null);
    

    but you can apply the same idea to your code. You can then use the OAuth token with Google User Info API, as described in Using OAuth 2.0 for Login to verify the email and get the user's name.

    0 讨论(0)
提交回复
热议问题