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

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

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

但我也在使用 AppEngine 后端,需要用它来验证用户。为此,我需要传入用于进行身份验证的 emailAccount。

在集成 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 更友好的方式将用户身份注入(inject) AppEngine Cloud Endpoints 客户端。

最佳答案

为他人写下问题可以帮助畅通思维过程,这一点令人惊奇。

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

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

关于android - 如何使用 GoogleApiClient 为云端点客户端提供凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24960555/

相关文章:

java - 如果添加 Games.API,应用程序会崩溃

java - 无法启动 Activity ... java.lang.NullPointerException

android - 初始位置中的 FusedLocationClient 与 LocationManager

Grails spring-security-oauth-google : how to set up

c# - 用于桌面应用程序的 Google 电子表格 OAuth 2.0 - 任何人都可以*简单*地使用它吗?

android - 如何使用 JSON 将记录插入 SQL 数据库

android - 在android中调用bpel进程

Android 应用 DeviceAnywhere LogCat

android - 如果内部布局有边距,则 scrollview 不工作

android - 谷歌登录使用新的 GoogleSignInOptions 获取访问 token