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?
Scope for email is
Which needs
com.google.android.gms.common.Scopes.PLUS_LOGIN
too.So you can replace your scope as
And you can use
which scope to being used to read user information (i.e. name, first/ lastname, profile-image, gender etc.)
From https://developers.google.com/+/api/oauth:
Instead, it is suggested you either use the
profile
scope or thehttps://www.googleapis.com/auth/plus.login
scope (which is available asScopes.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:
or
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