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

相关问题