Question

I need to get access token and send it to the server. With that access token server should get all user details, like name, profile picture and email.

I can get access token using Scopes.PLUS_LOGIN and Scopes.PLUS_ME, but with that access token server can't get user email.

Here is my code:

@Override
public void onConnected(Bundle arg0) {
    mSignInClicked = false;


    AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String token = null;
            String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME;
            try {
                token = GoogleAuthUtil.getToken(
                        getApplicationContext(),
                        Plus.AccountApi.getAccountName(mGoogleApiClient),
                        scope);
                appUser.setToken(token);
            } catch (IOException transientEx) {
                // Network or server error, try later
                Log.e(TAG, transientEx.toString());
            } catch (UserRecoverableAuthException e) {
                // Recover (with e.getIntent())                
            } catch (GoogleAuthException authEx) {
                // The call is not ever expected to succeed
                // assuming you have already verified that 
                // Google Play services is installed.
                Log.e(TAG, authEx.toString());
            }

            return token;
        }

        @Override
        protected void onPostExecute(String token) {
            Log.i(TAG, "Access token retrieved:" + appUser.getToken());
            // Get user's information
        }

    };


}

Does anybody know how to solve this problem?

Was it helpful?

Solution

You are missing the scope

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

I tested the other scopes and only that one appears to return the users email. You can test the different scopes and what they return here: People: get.

Note: I'm not an android programmer, you will probably have better luck finding out how to request that scope with android. I am looking, but haven't been able to find it.

Looks like the scope might just be email https://developers.google.com/+/api/oauth#email

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top