5

La mia app consente agli utenti di accedere con Google Plus, e ottiene il loro nome e indirizzo email. Sono tentato di accedere al token.Token di accesso recuperato: null. com.google.android.gms.auth.GoogleAuthException: Unknown

codice per accedere al 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(); 

Errore:

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 

Ho provato varie soluzioni che ho trovato su StackOverflow. Al momento, sto utilizzando l'ID cliente da "Account servizio" da dev console, ho anche provato a utilizzarlo per "Client ID per l'applicazione Android", ha ancora mostrato lo stesso errore.

Per favore fatemi sapere cosa sto sbagliando? Grazie.

risposta

5

ho risolto questo problema sostituendo questa linea

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

con questo

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

ho ottenuto il token di accesso, e mi chiedevo se sto facendo bene. Quindi ho verificato se il token era corretto. Ho provato ad andare a questo indirizzo, ho sostituito accessToken con il token che ho recuperato.

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

Mi ha mostrato questo tipo di 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" 
    } 

Nel issued_to mi ha mostrato il mio ID client per applicazioni Android che significa che questo token viene rilasciato al mio cliente id. Quindi presumo di essere sulla strada giusta.