如何使用GoogleApiClient为云端点客户端提供凭据

时间:2014-07-25 16:43:40

标签: android google-play-services google-oauth google-cloud-endpoints

好的,使用Google Play服务的GoogleApiClient,我让用户选择一个帐户(如果有多个帐户)并确认我的Android应用程序的oauth权限。我需要这个用于排行榜和其他一些事情。

但我也在使用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. 或者是否有另一种将用户身份注入AppEngine Cloud Endpoints客户端的GoogleApiClient友好方式。

1 个答案:

答案 0 :(得分: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);
}