What scope do I need to add for getting user email

2019-06-10 03:12发布

问题:

I am trying to get a access token from the google plus api.

I run the following piece of code:

private class RetrieveTokenTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String accountName = params[0];
        Log.e("accountName", accountName);
        String scopes = "oauth2:" + Scopes.PLUS_ME; //

        String token = null;
        try {
            token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, scopes);
        } catch (IOException e) {
            Log.e("GOOGLE+", e.getMessage());
        } catch (UserRecoverableAuthException e) {
            startActivityForResult(e.getIntent(), RC_SIGN_IN);
        } catch (GoogleAuthException e) {
            Log.e("GOOGLE+", e.getMessage());
        }
        return token;
    }

    @Override
    protected void onPostExecute(String s) {
        Log.d("TOKEN", "token: " + s);
    }
}

I check my access token in postman with a HTTP GET to https://www.googleapis.com/oauth2/v1/userinfo with header Authorization Bearer

Result:

{
id: "111111111111"
name: "my name"
given_name: "my"
family_name: "name"
link: https://plus.google.com/123456789
picture: https:....jpg
gender: "male"
}

How can I get the user's email? What scope do I need to use?

回答1:

From https://developers.google.com/+/api/oauth:

The https://www.googleapis.com/auth/plus.me scope is not recommended as a login scope because, for users who have not upgraded to Google+, it does not return the user's name or email address.

Instead, it is suggested you either use the profile scope or the https://www.googleapis.com/auth/plus.login scope (which is available as Scopes.PLUS_LOGIN).

Additionally you will need the email scope (this is the full name of the scope, it is not in URL format) so the email address will be returned as part of the call.

Your scopes definition might look something like:

String scopes = "oauth2:email " + Scopes.PLUS_LOGIN;

or

String scopes = "oauth2:profile email"; 

The https://www.googleapis.com/oauth2/v1/userinfo endpoint has been deprecated, works sporadically, and is scheduled to be removed in September.

Instead, you should use https://www.googleapis.com/plus/v1/people/me (the "me" represents the userid for the authenticated user) and provide the Authorization header with the same Bearer token you were before.

For more information about the deprecation and migration, see https://developers.google.com/+/api/auth-migration



回答2:

Scope for email is

https://www.googleapis.com/auth/userinfo.email

Which needs com.google.android.gms.common.Scopes.PLUS_LOGIN too.


So you can replace your scope as

private static final String SCOPE = "oauth2:"
        + TextUtils.join(" ", new String[] { Scopes.PLUS_LOGIN,
                "https://www.googleapis.com/auth/userinfo.email",
                Scopes.PLUS_ME });

And you can use

https://www.googleapis.com/auth/userinfo.profile

which scope to being used to read user information (i.e. name, first/ lastname, profile-image, gender etc.)