Ryan Ye
Ryan Ye

Reputation: 3259

GoogleAuthUtil.getToken gives com.google.android.gms.auth.GoogleAuthException: Unknown

My goal is getting user's basic profile information (first name, last name etc) without any server-side work. And I don't want to use a Google+ sign-in button on the UI.

I could get user's email without any problems. So the next step is to get an access token to fetch the profile, here is my code

    String token = null;
    try {
        token = GoogleAuthUtil.getToken(getApplicationContext(), email, SCOPE);
    } catch (final UserRecoverableAuthException e) {
        startActivityForResult(e.getIntent(), REQUEST_CODE_AUTH_GOOGLE_ACCOUNT);
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    } catch (GoogleAuthException e) {
        Log.e(TAG, e.toString());
    }

and I've defined SCOPE

private static final String SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile";

When I run my app, I got "com.google.android.gms.auth.GoogleAuthException: Unknown" exception. I've also tried other scopes like Scope.PLUS_LOGIN, Scope.PLUG_PROFILE. But all of them gave me the same exception.

Btw, in Google API Console, I've already setup a client ID.

I've been stuck on this for 2 days. Any help would be really appreciated.

Upvotes: 3

Views: 4018

Answers (1)

Madhur Ahuja
Madhur Ahuja

Reputation: 22681

I think you are doing it wrong. That's the way I do it:

scopes.add(AnalyticsScopes.ANALYTICS_READONLY);

                credential = GoogleAccountCredential.usingOAuth2(this, scopes);
                if (TextUtils.isEmpty(appPreferences.getUserName()))
                {
                        try
                        {

                                startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
                        }
                        catch (ActivityNotFoundException e)
                        {

                                Toast.makeText(this, getString(R.string.gps_missing), Toast.LENGTH_LONG).show();

                                return;
                        }

                }

Take a look at the my source file here: https://github.com/madhur/GAnalytics/blob/develop/src/in/co/madhur/ganalyticsdashclock/MainActivity.java

Upvotes: 1

Related Questions