Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newly released "Authentication with Google Play Services", Problems with getting a token via GoogleAuthUtil.getToken

Tags:

android

token

api

Update:

Just now I signed up to Google API with the Google account I'm also using for the Google App Engine and now the error has changed to com.google.android.gms.auth.GoogleAuthException: Unknown


Google just released a new way to sign up to one's app via a Google Account and has given some explanation in their blogpost, too.

I'm writing an Android app you have to sign up for to create a user account and I'm using Google App Engine for the back end. With the play services, I want to get the users email affiliated with their Android device and a token. I can already get the email, however, getting the token only throws errors and due to the documentation being quite sparse, I don't know how to fix these. That's how I get the email:

private String[] getAccountNames() {
    mAccountManager = AccountManager.get(this);
    Account[] accounts = mAccountManager.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
    names = new String[accounts.length];
    for (int i = 0; i < names.length; i++) {
        names[i] = accounts[i].name;
    }

    return names;
}

However, when I'm calling token = GoogleAuthUtil.getToken(context, email, "scope", it gives me these errors: GooglePlayServices not available due to error 1 and com.google.android.gms.auth.GooglePlayServicesAvailabilityException: GooglePlayServicesNotAvailable.

Now my question is, do I have to set any permissions or include any libraries to fix that? And what is the scope parameter? An example for how to get a token and what to put in for scope is given in their blogpost to set the scope variable to "https://www.googleapis.com/auth/userinfo.profile" but I still get the same error unfortunately.

I also registered my app for the Google API console and enabled the G+ API, do I have to configure something else there, too?

like image 935
Marius Hilarious Avatar asked Oct 02 '12 12:10

Marius Hilarious


3 Answers

You first need to pass the correctly formatted scope, like this:

  private final static String G_PLUS_SCOPE = 
      "oauth2:https://www.googleapis.com/auth/plus.me";
  private final static String USERINFO_SCOPE =   
      "https://www.googleapis.com/auth/userinfo.profile";
  private final static String SCOPES = G_PLUS_SCOPE + " " + USERINFO_SCOPE;

You also need to install Google Play services from here, https://play.google.com/store/apps/details?id=com.google.android.gms. Google Play services has started to roll out to all devices with Google Play installed (Froyo+).

There is more information provided here: http://android-developers.blogspot.com/2012/09/google-play-services-and-oauth-identity.html

like image 45
Chirag Shah Avatar answered Nov 10 '22 03:11

Chirag Shah


I had the same problem using the Google Client Library for Google Drive. The solution is pretty easy. Just prepend "oauth2:" to your scopeUrl.

Wrong:

GoogleAuthUtil.getToken(this, account.name, DriveScopes.DRIVE);

Right:

GoogleAuthUtil.getToken(this, account.name, "oauth2:" + DriveScopes.DRIVE);
like image 113
Basic Coder Avatar answered Nov 10 '22 04:11

Basic Coder


You can only get a token for Google services that support OAuth2. If you are writing your own webservice, you should authenticate to it by using its native authentication mechanism (username and password?). "Scope" is obviously an invalid scope, but it seems that you don't have Google Play Services installed, and that is the cause of your error. You can install it from the Play Store, but it won't really work for you, since your are not using a Google service.

like image 41
Nikolay Elenkov Avatar answered Nov 10 '22 02:11

Nikolay Elenkov