2013-08-07 18 views
6

Czy ktoś może mi powiedzieć, co robię źle? Muszę dostać token dostępu z Google Plus ..Uzyskaj token dostępu z google plus Android

kładę to w moim sposobie onConnected() ale ja nie otrzymuję token dostępu, zamiast otrzymuję błąd ...

Kod:

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(); 
    } 

błąd:

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

Czy ktoś może mi powiedzieć, jaki byłby właściwy sposób, aby uzyskać dostęp do tokenu Google Plus od użytkownika?

+1

Zrób to w wątku tła Asynktask na przykład – user1940676

+1

Odpowiedź jest w błędzie? Stwórz wątek, który wykonuje werset, aby Twoja aplikacja nie zawiesiła się podczas negocjowania – Tschallacka

+1

Po umieszczeniu w Asynctask doInBackground, pojawia się następujący komunikat: Metoda getToken (Context, String, String) w typie GoogleAuthUtil nie ma zastosowania dla argumentów (nowa AsyncTask () {}, nowa AsyncTask () {}, nowa AsyncTask () {}) – Karlis

Odpowiedz

1

Oto kod można użyć. Jeśli ktoś ma lepszą sugestię, to proszę napisać:

 /** 
    * 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

Musisz go pobrać za pomocą zadania asynchronicznego.

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; } 

Twój token dostępu zostanie zapisany w zmiennej tokenów.

4

Dostęp do tokena można uzyskać za pomocą metody onConnected(). dodaj ten kod do zakresu metod Connected().

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

Idealny. Pracował dla mnie – Arshad

Powiązane problemy