5

好的,使用Google Play服務的GoogleApiClient我已讓用戶選擇一個帳戶(如果有多個)並確認我的Android應用的oauth權限。我需要這個排行榜和其他一些東西。如何使用GoogleApiClient爲雲終端客戶端提供憑證

但我也在使用一個AppEngine後端,並需要使用它來認證用戶。爲此,我需要傳入用於驗證的emailAccount。

在集成Google Play服務之前,我手動處理了帳戶選擇,因此我總是可以訪問用戶選擇的emailAccount。但是GoogleApiClient可以處理這個問題。

private void signInToGoogleAccount() { 
    googleApiClient = new GoogleApiClient.Builder(this) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(Plus.API) 
     .addScope(Plus.SCOPE_PLUS_LOGIN) // Profile + Circles + ?writing app activities? 
     .build(); 
    googleApiClient.connect(); 
} 

// GPS Connection Callbacks. 

@Override 
public void onConnected(Bundle bundle) { 
    Log.i(TAG, "#onConnected - GoogleApiClient connected!!"); 
    if (Plus.PeopleApi.getCurrentPerson(googleApiClient) != null) { 
     final Person currentPerson = Plus.PeopleApi.getCurrentPerson(googleApiClient); 
     String personName = currentPerson.getDisplayName(); 
     Log.i(TAG, "name=" + personName); 
     String personGooglePlusProfile = currentPerson.getUrl(); 
     Log.i(TAG, "profileUrl=" + personGooglePlusProfile); 
    } 
} 

@Override 
public void onConnectionSuspended(int cause) { 
    Log.d(TAG, "#onConnectionSuspended - GoogleApiClient connection suspend. cause=" + cause); 
    googleApiClient.connect(); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == RC_SIGN_IN) { 
     Log.i(TAG, "#onActivityResult RC_SIGN_IN resultCode=" + resultCode + " data=" + data); 
     intentInProgress = false; 
     if (resultCode == RESULT_OK) { 
      if (!googleApiClient.isConnecting()) { 
       googleApiClient.connect(); 
      } 
     } else if (resultCode == RESULT_CANCELED) { 
      Log.i(TAG, "#onActivityResult RC_SIGN_IN user cancelled"); 
     } else { 
      Log.w(TAG, "#onActivityResult RC_SIGN_IN something weird"); 
     } 
    } 
} 

private void doRemoteTask() { 
    final GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context, AppConstants.AUDIENCE); 

    // This is the info that I need to recover from the GooglePlayServices signin. 
    credential.setSelectAccountName(userAccountName); 

    final MyRemoteTask myRemoteTask = new MyRemoteTask.Builder(
     AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential 
    ).build(); 
    myRemoteTask.doThing(someArg); 
} 

所以我需要知道的是:

  1. 我如何獲得的電子郵件帳戶的用戶所選擇的細節,當我使用GoogleApiClient授權。
  2. 或者還有另外一種GoogleApiClient友好的方式將用戶身份注入到AppEngine Cloud Endpoints客戶端中。

回答

3

令人驚訝的是如何爲其他人寫出問題可以幫助解除思想過程。

解決方案是使用Plus.AccountApi.getAccountName(googleApiClient);

@Override 
public void onConnected(Bundle bundle) { 
    final String accountName = Plus.AccountApi.getAccountName(googleApiClient); 
    Log.i(TAG, "#onConnected - GoogleApiClient accountName=" + accountName); 
} 
+2

感謝分享。爲我節省了一些時間。起初解決方案肯定不明顯。 – sthomps

+2

'AccountApi'已棄用! 請更新您的代碼。 – GFPF

相關問題