android - GoogleApiClient 有一个可选的 Plus.API,并且没有连接到带有 Play Services 8.3 中引入的新 Google 标志的 Plus

标签 android google-play-services google-api-client google-signin google-plus-signin

当我尝试使用 Plus 的 People Api 访问用户的基本个人资料信息时,我收到了很多关于我的应用程序崩溃的报告。

这是我的 GoogleApiClient 设置

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .requestScopes(new Scope(Scopes.PROFILE))
                    .requestScopes(new Scope(Scopes.PLUS_LOGIN))
                    .requestIdToken(<id_token>)
                    .requestProfile()
                    .build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .addApi(Plus.API)
                    .addConnectionCallbacks(this)
                    .build();

在此行获取 IllegalStateException:

Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClientPlus)

java.lang.IllegalStateException:GoogleApiClient 有一个可选的 Plus.API 并且没有连接到 Plus。使用 GoogleApiClient.hasConnectedApi(Plus.API) 保护此调用。

只有在 google 登录返回登录用户后才会调用此代码。那为什么plus api连接不上呢?

最佳答案

protected synchronized void buildGoogleApiClient() {
    // Configure sign-in to request the user's ID, email address, and basic profile.
    // ID and basic profile are included in DEFAULT_SIGN_IN.
    gGoogleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestScopes(new Scope(Scopes.PROFILE))
            .requestScopes(new Scope(Scopes.PLUS_LOGIN))
            .requestProfile()
            .requestEmail()
            .build();

    // Build a GoogleApiClient with access to the Google Sign-In API
    // and the options specified by gGoogleSignInOptions.
    gGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, mGoogleSignInOptions)
            .addApi(Plus.API)
            .build();
}


@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        if (resultCode == Activity.RESULT_OK) {
            // Google+
            if (gGoogleApiClient.hasConnectedApi(Plus.API))
                Person person = Plus.PeopleApi.getCurrentPerson(gGoogleApiClient);
            else
                Log.e("TAG", "Google+ not connected");

            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        } else 
            Log.e("TAG", "Result Cancelled");
    }
}

关于android - GoogleApiClient 有一个可选的 Plus.API,并且没有连接到带有 Play Services 8.3 中引入的新 Google 标志的 Plus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34353674/

相关文章:

ruby-on-rails - Google Oauth SSL错误-SSL_connect返回= 1 errno = 0状态= SSLv3读取服务器证书B : certificate verify failed

android - 带有 AppCompat 主题的日期选择器对话框

png - Android 构建 apk 更改原始 png 文件

android - Android 中的 Excel 工作表显示

java - 将 google 服务从 9.0.0 更新到 10.0.1 Android studio 2.2

react-native - Firebase - Gradle 构建失败 - com.google.android.gms :play-services-measurement-base

Android GoogleApiClient.Builder().enableAutoManage 导致 IllegalArgumentException : Can only use lower 16 bits for requestCode

android - 在 UI 线程中执行一些 SQLite 查询是否正确?

android - 将 Google 登录集成到您的 Android 应用程序中时出现错误

android - 如何撤销/取消授予 Android 设备上的应用程序的帐户授权