2013-08-07 19 views
6

Qualcuno può dirmi cosa sto sbagliando? Ho bisogno di ottenere il token di accesso da Google Plus ..Ottieni il token di accesso da google plus Android

Ho messo questo nel mio metodo onConnected() ma io non sto ottenendo il token di accesso, invece io sono sempre errore ...

Codice:

try { 
     String token = GoogleAuthUtil.getToken(this, mPlusClient.getAccountName() + "", "oauth2:" + Scopes.PLUS_PROFILE + 
           "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"); 
     Log.d("AccessToken", token); 
    } catch (UserRecoverableAuthException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (GoogleAuthException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

Errore:

08-07 10:10:24.199: E/GoogleAuthUtil(17203): Calling this from your main thread can lead to deadlock and/or ANRs 

qualcuno può dirmi quale sarebbe il modo corretto per ottenere il token di accesso Google Plus da parte dell'utente?

+1

Farlo in un thread in background Asynctask ad esempio – user1940676

+1

La risposta è nell'errore? Crea una discussione che faccia la verbalizzazione in modo che la tua app non si blocchi mentre sta negoziando – Tschallacka

+1

Quando inserisco Asynctask doInBackground, ottengo il seguente messaggio - Il metodo getToken (Context, String, String) nel tipo GoogleAuthUtil non è applicabile per gli argomenti (nuovo AsyncTask () {}, nuovo AsyncTask () {}, nuovo AsyncTask () {}) – Karlis

risposta

1

Ecco il codice è possibile utilizzare. Se qualcuno ha un suggerimento migliore, si prega di inviare:

 /** 
    * Successfully connected (called by PlusClient) 
    */ 
    @Override 
    public void onConnected(Bundle connectionHint) { 
     /* First do what ever you wanted to do in onConnected() */ 
     .... 
     .... 

     /* Now get the token using and async call*/ 
     GetGooglePlusToken token = new GetGooglePlusToken(this.getActivity(), mPlusClient); 
     token.execute(); 

    } 



class GetGooglePlusToken extends AsyncTask<Void, Void, String> { 
    Context context; 
    private GoogleApiClient mGoogleApiClient; 
    private String TAG = this.getClass().getSimpleName(); 

    public GetGooglePlusToken(Context context, GoogleApiClient mGoogleApiClient) { 
     this.context = context; 
     this.mGoogleApiClient = mGoogleApiClient; 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     String accessToken1 = null; 
     try { 

      Bundle bundle = new Bundle(); 

      String accountname = Plus.AccountApi.getAccountName(mGoogleApiClient); 
      String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + "https://www.googleapis.com/auth/userinfo.email" + " https://www.googleapis.com/auth/plus.profile.agerange.read"; 
      accessToken1 = GoogleAuthUtil.getToken(context, 
        accountname, 
        scope); 
      return accessToken1; 

     } 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. 
      //TODO: HANDLE 
      Log.e(TAG, "transientEx"); 
      transientEx.printStackTrace(); 
      accessToken1 = null; 

     } catch (UserRecoverableAuthException e) { 
      // Recover 
      Log.e(TAG, "UserRecoverableAuthException"); 
      e.printStackTrace(); 
      accessToken1 = null; 

     } catch (GoogleAuthException authEx) { 
      // Failure. The call is not expected to ever succeed so it should not be 
      // retried. 
      Log.e(TAG, "GoogleAuthException"); 
      authEx.printStackTrace(); 
      accessToken1 = null; 
     } catch (Exception e) { 
      Log.e(TAG, "RuntimeException"); 
      e.printStackTrace(); 
      accessToken1 = null; 
      throw new RuntimeException(e); 
     } 
     Log.wtf(TAG, "Code should not go here"); 
     accessToken1 = null; 
     return accessToken1; 
    } 

    @Override 
    protected void onPostExecute(String response) { 
     Log.d(TAG, "Google access token = " + response); 
    } 
} 
2

È necessario recuperarlo utilizzando un'attività asincrona.

public void onConnected(Bundle connectionHint) { 
// Reaching onConnected means we consider the user signed in. 
Log.i(TAG, "onConnected"); 

// Update the user interface to reflect that the user is signed in. 
mSignInButton.setEnabled(false); 
mSignOutButton.setEnabled(true); 
mRevokeButton.setEnabled(true); 

// Retrieve some profile information to personalize our app for the user. 
Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); 


AsyncTask<Void, Void, String > task = new AsyncTask<Void, Void, String>() { 
    @Override 
    protected String doInBackground(Void... params) { 
     String token = null; 
     final String SCOPES = "https://www.googleapis.com/auth/plus.login "; 

     try { 
      token = GoogleAuthUtil.getToken(
        getApplicationContext(), 
        Plus.AccountApi.getAccountName(mGoogleApiClient), 
        "oauth2:" + SCOPES); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (GoogleAuthException e) { 
      e.printStackTrace(); 
     } 


     return token; 

    } 

    @Override 
    protected void onPostExecute(String token) { 
     Log.i(TAG, "Access token retrieved:" + token); 
    } 

}; 
task.execute(); 


System.out.print("email" + email); 
mStatus.setText(String.format(
     getResources().getString(R.string.signed_in_as), 
     currentUser.getDisplayName())); 

Plus.PeopleApi.loadVisible(mGoogleApiClient, null) 
     .setResultCallback(this); 

// Indicate that the sign in process is complete. 
mSignInProgress = STATE_DEFAULT; } 

Il token di accesso verrà memorizzato nella variabile token.

4

È possibile accedere a token nel metodo onConnected(). aggiungi questo ambito di metodi onConnected().

final String SCOPES = "https://www.googleapis.com/auth/userinfo.profile"; 
     new AsyncTask<Void, Void, Void>() { 
      @Override 
      protected Void doInBackground(Void... params) { 
       String ace = ""; 
       try { 
        ace = GoogleAuthUtil.getToken(getApplicationContext(), 
               Plus.AccountApi.getAccountName(mGoogleApiClient), 
               "oauth2:" + SCOPES); 
       } 
       catch (IOException e) { 
        e.printStackTrace(); 
       } 
       catch (GoogleAuthException e) { 
        e.printStackTrace(); 
       } 
       Log.i("", "mustafa olll " + ace); 
       return null; 
      } 
     }.execute(); 
+0

Perfetto. Ha funzionato per me – Arshad

Problemi correlati