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

2023-12-27

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

但我还使用应用程序引擎后端,需要使用该后端对用户进行身份验证。为此,我需要传递用于进行身份验证的电子邮件帐户。

在集成 Google Play 服务之前,我手动处理帐户选择,因此我始终可以访问用户选择的电子邮件帐户。但 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 客户端。

令人惊奇的是,为他人写下问题可以帮助疏通思维过程。

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

@Override
public void onConnected(Bundle bundle) {
    final String accountName = Plus.AccountApi.getAccountName(googleApiClient);
    Log.i(TAG, "#onConnected - GoogleApiClient accountName=" + accountName);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 GoogleApiClient 为云端点客户端提供凭据 的相关文章

随机推荐