1
votes

In my android application I request google plus token which I send to server. I want to be able to get user information including his email address in server.

Here's a call that works but doesn't give me email:

GoogleAuthUtil.getToken(activity, Plus.AccountApi.getAccountName(googleApiClient), "oauth2:" + Scopes.PLUS_ME + " " + Scopes.PLUS_LOGIN + " " + Scopes.PROFILE);

With it I only get this information:

{

    "id": "11213535252359196332836",
    "name": "Name Surname",
    "given_name": "Name",
    "family_name": "Surname",
    "link": "https://plus.google.com/+laskdjfksdjf",
    "picture": "https://asdfsdafasdfasdfasdfasdfasdfsdaf7c/photo.jpg",
    "gender": "male",
    "locale": "en"

}

I found that I need to ask for userinfo.email permission, so I modified my call:

GoogleAuthUtil.getToken(activity, Plus.AccountApi.getAccountName(googleApiClient), "oauth2:" + Scopes.PLUS_ME + " " + Scopes.PLUS_LOGIN + " " + Scopes.PROFILE + " https://www.googleapis.com/auth/userinfo.email");

The problem is, that now getToken() call returns me this error:

09-16 18:53:57.311: W/System.err(26467): com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
09-16 18:53:57.316: W/System.err(26467):    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
09-16 18:53:57.316: W/System.err(26467):    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
09-16 18:53:57.316: W/System.err(26467):    at lt.sm.discountcity.GPlus.getServerOnlineToken(GPlus.java:237)
09-16 18:53:57.316: W/System.err(26467):    at lt.sm.discountcity.fragments.LoginFragment$5$1.runOnSeparateThread(LoginFragment.java:173)
09-16 18:53:57.316: W/System.err(26467):    at lt.smtools.tasks.TaskManager$2.run(TaskManager.java:139)
09-16 18:53:57.316: W/System.err(26467):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-16 18:53:57.316: W/System.err(26467):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-16 18:53:57.316: W/System.err(26467):    at java.lang.Thread.run(Thread.java:841)

Any ideas what am I doing wrong and how to solve this?

1
I've seen first post and I don't understand how second post is dublicate, because it talks about getting friends email, not the user that has logged in. First post solution puts me in an infinite loop. - SMGhost

1 Answers

0
votes

http://developer.android.com/reference/com/google/android/gms/auth/GoogleAuthUtil.html

void getAndUseAuthTokenBlocking() {
   try {
      // Retrieve a token for the given account and scope. It will always return either
      // a non-empty String or throw an exception.
      final String token = GoogleAuthUtil.getToken(Context, String, String)(context, email,      scope);
      // Do work with token.
      ...
      if (server indicates token is invalid) {
          // invalidate the token that we found is bad so that GoogleAuthUtil won't
          // return it next time (it may have cached it)
          GoogleAuthUtil.invalidateToken(Context, String)(context, token);
          // consider retrying getAndUseTokenBlocking() once more
          return;
      }
      return;
   } catch (GooglePlayServicesAvailabilityException playEx) {
     Dialog alert = GooglePlayServicesUtil.getErrorDialog(
         playEx.getConnectionStatusCode(),
         this,
         MY_ACTIVITYS_AUTH_REQUEST_CODE);
     ...
   } catch (UserRecoverableAuthException userAuthEx) {
      // Start the user recoverable action using the intent returned by
      // getIntent()
      myActivity.startActivityForResult(
              userAuthEx.getIntent(),
              MY_ACTIVITYS_AUTH_REQUEST_CODE);
      return;
   } catch (IOException transientEx) {
      // network or server error, the call is expected to succeed if you try again later.
      // Don't attempt to call again immediately - the request is likely to
      // fail, you'll hit quotas or back-off.
      ...
      return;
   } catch (GoogleAuthException authEx) {
      // Failure. The call is not expected to ever succeed so it should not be
      // retried.
      ...
      return;
   }
   }