【问题标题】:GoogleApiClient has an optional Plus.API and is not connected to Plus with new Google sign introduced in Play Services 8.3GoogleApiClient 有一个可选的 Plus.API,并且没有通过 Play Services 8.3 中引入的新 Google 符号连接到 Plus
【发布时间】:2015-12-18 10:38:37
【问题描述】:

当我尝试使用 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没有连接呢?

【问题讨论】:

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


    【解决方案1】:
    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");
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用以下方法(当然,请确保您的 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。希望对您有所帮助!

      【讨论】:

      • 您正在请求 Plus 登录范围,但是当您将 Plus Api 添加到 Google API 客户端时,它不会自动处理吗?
      • 感谢分享链接。我将该范围添加到 gso。但是,第一次登录时,当用户被询问登录流程中的权限时,它仍然会崩溃并出现相同的异常。我断开应用程序并重试,它可以工作并且没有向我显示权限对话框。但它应该显示权限对话框,就像它是第一次登录一样。因此,该应用程序没有断开连接,我无法再次测试。这里有什么提示吗?
      • 确保您的 Google 帐户已有 Google+ 个人资料。 with the same exception when logging in for the first time 有什么例外?还有一件事,也许您应该从手机中删除该 Google 帐户,然后使用您的应用重新添加
      • 是的,我的帐户有一个 g+ 个人资料。同样的例外,我的意思是问题中提到的 IllegalStateException。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 2016-12-30
      • 2016-02-22
      • 2016-09-25
      相关资源
      最近更新 更多