Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access token retrieved:null. com.google.android.gms.auth.GoogleAuthException: Unknown

My app allows users to login with Google Plus, and gets their name, and email address. I am tried to access the token.

Code to access the token:

Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();        
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
        AccountManager am = AccountManager.get(this);

        final Account[] accounts = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);

        AsyncTask<Void, Void, String> task2 = new AsyncTask<Void, Void, String>() {
            public static final int REQUEST_CODE_TOKEN_AUTH = 100;

            @Override
            protected String doInBackground(Void... params) {
                String mScope="audience:server:client_id:899555500747-38rpnq51of946grhdvofck7r8u5p09cd.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/plus.login";
// Get the token for the current user
                String token = null;
                try {
                    token = GoogleAuthUtil.getToken(getApplicationContext(), Plus.AccountApi.getAccountName(mGoogleApiClient), mScope);
                    Log.i("G token", token);
                } catch (IOException transientEx) {
                    // Network or server error, try later
                    Log.e(TAG, transientEx.toString());
                } catch (UserRecoverableAuthException e) {
                    // Recover (with e.getIntent())
                    Log.e(TAG, e.toString());
                    Intent recover = e.getIntent();
                    startActivityForResult(recover, REQUEST_CODE_TOKEN_AUTH );
                } 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:" + token);
            }

        };
        task2.execute();

Error:

01-27 23:42:14.877  30994-31262/com.unicloud.mittal E/loginWithGooglePlus﹕ com.google.android.gms.auth.GoogleAuthException: Unknown
01-27 23:42:14.877  30994-30994/com.unicloud.mittal I/loginWithGooglePlus﹕ Access token retrieved:null

I have tried various solutions which I could find on stackoverflow. At the moment, I am using Client ID from "Service Account" from dev console, I have also tried using it for "Client ID for Android Application", it still showed the same error.

Please let me know what am I doing wrong? Thanks.

like image 751
user2798227 Avatar asked Jan 27 '15 23:01

user2798227


1 Answers

I solved this question by replacing this line

String mScope="audience:server:client_id:899555500747-38rpnq51of946grhdvofck7r8u5p09cd.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/plus.login";

with this

String mScope = "oauth2:https://www.googleapis.com/auth/plus.login";

I got the access token, and I was wondering if I am doing right. So I verified if the token was correct. I tried to go to this address, I replaced accessToken with the token I retrieved.

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=accessToken

It showed me this kind of output.

{
  "issued_to": "xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
     "audience": "xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
     "user_id": "xxxxxxxxxxxxxxxxxxxxxxx",
     "scope": "https://www.googleapis.com/auth/userinfo.profile https://gdata.youtube.com",
     "expires_in": 3019,
     "access_type": "online"
    }

In the issued_to it showed me my Client ID for Android Application that means this token is issued to my client id. So I assume I am on the right track.

like image 80
user2798227 Avatar answered Sep 24 '22 07:09

user2798227