取消时Android Google登录错误

时间:2018-08-02 14:08:21

标签: android google-authentication

我遇到了一些无法解决的怪异问题。我有一个具有Google登录功能的Android应用。该应用程序遵循MVP模式,并使用片段而不是活动。登录部分是从这里: https://developers.google.com/identity/sign-in/android/

一切正常,直到我尝试通过点击其外部来关闭登录对话框(在其中选择用户)。我认为它应该给我一个状态代码12501(SIGN_IN_CANCELLED),但是我却得到了代码13(ERROR)。我的假设错了吗?我是否应该忽略代码13,并且在发生错误时不向用户显示任何错误?

以下是最相关的代码

活动:

public class SignInActivity extends AppCompatActivity implements SignInFragment.SignInListener {    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);

        // Add sign in fragment
        mSignInFragment = new SignInFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager
                .beginTransaction()
                .replace(R.id.sign_in_frame, mSignInFragment, TAG)
                .commit();
    }

    @Override
    public void startSignIn() {
        Intent signInIntent = mSignInFragment.getGoogleSignInClient().getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

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

        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            mSignInFragment.handleSignInResult(task);
        }
    }
}

片段:

public class SignInFragment extends DaggerFragment implements SignInContract.View, GoogleApiClient.OnConnectionFailedListener {

    private SignInFragment.SignInListener signInListener;

    public interface SignInListener {
        void startSignIn();
        void startMainActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken( clientId )
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(getActivity(), gso);

        // Calling this to force user select dialog to appear everytime
        mGoogleSignInClient.signOut();

        signInButton.setOnClickListener(v -> {
            signInListener.startSignIn();
        }
    }

    protected void handleSignInResult(Task<GoogleSignInAccount> task) {
        try {
            GoogleSignInAccount acct = task.getResult(ApiException.class);
            String idToken = acct.getIdToken();
            presenter.deliverTokenToServer(idToken);

        } catch (ApiException e) {
            e.printStackTrace();
            Log.d(TAG, "handleSignInResult:" + e.getStatusCode());
            Snackbar snackbar = Snackbar.make(getView(), R.string.error_login_failed_try_again, Snackbar.LENGTH_LONG);
            snackbar.show();

        }
    }
}

在gradle文件中,我使用了'com.google.android.gms:play-services-auth:15.0.1''com.google.gms:google-服务:4.0.1'

2 个答案:

答案 0 :(得分:2)

我终于找到了。 这是Google Play库中的回归。它已在play-services-auth:16.0.0中修复。 这是一些details

implementation com.google.android.gms:play-services-auth:16.0.0

答案 1 :(得分:-1)

根据博客,这是Google SignIn当前库中的一个错误,将在下一个更新中自动修复。 但是,当前的最新库版本是:

com.google.android.gms:play-services-identity:15.0.1

参考:Google Play Gradles