GoogleApiClient具有可选的Plus.API,并且未与Play服务8.3中引入的新Google标志连接到Plus

时间:2015-12-18 10:38:37

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

当我尝试使用Plus'Persion 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登录返回后调用。那么为什么不能连接api?

2 个答案:

答案 0 :(得分:5)

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");
    }
}

答案 1 :(得分:1)

您可以尝试使用以下内容(当然,请确保您的Google帐户已有Google+个人资料):

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestScopes(new Scope(Scopes.PLUS_LOGIN))
                .requestEmail()
                .build();


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

请查看my sample project at GitHub。希望它有所帮助!