【问题标题】:GoogleApiClient: Games.API and Auth.GOOGLE_SIGN_IN_API not working togetherGoogleApiClient:Games.API 和 Auth.GOOGLE_SIGN_IN_API 不能一起工作
【发布时间】:2018-02-02 13:16:06
【问题描述】:

我有一个使用谷歌帐户登录的有效应用程序。

我正在这样构建我的客户端

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
        .requestEmail()
        .requestProfile()
        .requestIdToken(getString(R.string.server_client_id))
        .build();

GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .addConnectionCallbacks(this)
                .build();

现在我决定添加Games.API,生成以下客户端:

GoogleApiClient  googleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Games.API)
        .addScope(Games.SCOPE_GAMES)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .addConnectionCallbacks(this)
        .build();

但是当我尝试连接客户端时

googleApiClient.connect();

我得到以下异常:

java.lang.IllegalStateException: Cannot use SIGN_IN_MODE_REQUIRED with GOOGLE_SIGN_IN_API. Use connect(SIGN_IN_MODE_OPTIONAL) instead.

所以我阅读了异常并将我的连接调用修改为

googleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);

所以现在我没有再遇到任何异常,但客户端由于某种原因永远不会连接。任何想法为什么客户端现在不连接?

编辑:完整的工作代码示例

package at.hakkon.space.signin;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.BaseGameUtils;

import at.hakkon.space.R;
import at.hakkon.space.activity.MainActivity;
import at.hakkon.space.application.ApplicationClass;



public class GoogleSignInActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {

    private static final String TAG = "T1_GSignInActivity";

    private static int RC_SIGN_IN = 9001;

    private boolean mResolvingConnectionFailure = false;
    private boolean mAutoStartSignInflow = true;
    private boolean mSignInClicked = false;

    private static GoogleApiClient googleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "GoogleSignInActivity");

        setupGoogleClient();
    }



    public void setupGoogleClient() {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        ApplicationClass.getInstance().setGoogleClient(googleApiClient);
    }


    @Override
    public void onStart() {
        super.onStart();
        googleApiClient.connect();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            mSignInClicked = false;
            mResolvingConnectionFailure = false;
            if (resultCode == RESULT_OK) {
                googleApiClient.connect();
            } else {
                // Bring up an error dialog to alert the user that sign-in
                // failed. The R.string.signin_failure should reference an error
                // string in your strings.xml file that tells the user they
                // could not be signed in, such as "Unable to sign in."
                BaseGameUtils.showActivityResultError(this,
                        requestCode, resultCode, R.string.sign_in_other_error);
            }
        }
    }

    private void startNextActivity() {
        finish();
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }


    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(this, "Google Client Connection failed:\n" + connectionResult.toString(), Toast.LENGTH_LONG ).show();

        if (mResolvingConnectionFailure) {
            // already resolving
            return;
        }

        // if the sign-in button was clicked or if auto sign-in is enabled,
        // launch the sign-in flow
        if (mSignInClicked || mAutoStartSignInflow) {
            mAutoStartSignInflow = false;
            mSignInClicked = false;
            mResolvingConnectionFailure = true;

            // Attempt to resolve the connection failure using BaseGameUtils.
            // The R.string.signin_other_error value should reference a generic
            // error string in your strings.xml file, such as "There was
            // an issue with sign-in, please try again later."

            if (!BaseGameUtils.resolveConnectionFailure(this, googleApiClient, connectionResult, RC_SIGN_IN, R.string.sign_in_other_error)) {
                mResolvingConnectionFailure = false;
                Toast.makeText(this, "Conflict NOT RESOLVED :((", Toast.LENGTH_LONG ).show();
            }else{
                Toast.makeText(this, "Conflict RESOLVED", Toast.LENGTH_LONG ).show();
                //startNextActivity();
            }

        }

        // Put code here to display the sign-in button

        //Log.d(TAG, "onConnectionFailed:" + connectionResult);
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Toast.makeText(this, "Google Client Connected.", Toast.LENGTH_LONG ).show();
        startNextActivity();
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this, "Google Client Connection Suspended.", Toast.LENGTH_LONG ).show();
    }
}

【问题讨论】:

    标签: android google-signin google-api-client


    【解决方案1】:

    使用

    mGoogleApiClient = new GoogleApiClient.Builder(activity)
                .addApi(Games.API)
                .addConnectionCallbacks(callbacks)
                .build();
    

    然后

    mGoogleApiClient.connect();
    

    如果您在控制台中正确配置了游戏 - 它将连接并访问游戏功能(例如成就和排行榜)

    class GoogleConnectionCallbacks implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
    
        @Override
        public void onConnected(@Nullable Bundle bundle) {
            MyLogger.d("DBG_GGL_LDB", "connected");
            Player p = Games.Players.getCurrentPlayer(mGoogleApiClient);
            String displayName;
            if (p == null) {
                Log.w("DBG_GGL_LDB", "mGamesClient.getCurrentPlayer() is NULL!");
                displayName = "???";
            } else {
                displayName = p.getDisplayName();
            }
            MyLogger.d("DBG_GGL_LDB", "Hello, " + displayName);
    
            if (mGoogleApiClient.hasConnectedApi(Games.API)) {
    
                MyLogger.d("DBG_GGL_LDB", "hasConnectedApi true - can user leaderboard");
            } 
        }
    
        @Override
        public void onConnectionSuspended(int i) {
            MyLogger.d("DBG_GGL_LDB", "connection suspended");
    
        }
    
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
            MyLogger.d("DBG_GGL_LDB", "connection failed " + connectionResult.getErrorMessage());
    
        }
    }
    

    这个问题困扰我很久了,都是因为使用.addApi(Auth.GOOGLE_SIGN_IN_API, gso)

    【讨论】:

    • 嘿伙计。感谢您的回复,但我发布的代码实际上工作得很好。我非常专注于我的测试设备,以至于我没有测试任何其他设备。结果发现其他设备都可以工作,在我在测试设备上重新安装 google play 后它也可以工作。
    【解决方案2】:

    该代码在我的测试设备以外的任何其他设备上都能正常运行。在我的测试设备上重新安装 Google Play 后,它也可以工作了!我不知道是什么导致了问题,但重新安装解决了它。

    原来我根本不需要 SIGN_IN。如果需要,请查看 Alexanders 的答案!

    【讨论】:

    • 你已经删除了 SIGN_IN api,这就是我认为它开始工作的原因,而不是因为重新安装
    • 嗯,你是对的,现在已经 2 个月了,所以细节很模糊。原来我不再需要登录。出于某种原因,我仍然必须重新安装 google play 才能正常工作。
    猜你喜欢
    • 2016-09-16
    • 2014-01-07
    • 2019-08-17
    • 2016-11-23
    • 2019-02-18
    • 2015-05-16
    • 2017-10-14
    • 2012-06-01
    • 2011-11-05
    相关资源
    最近更新 更多