我已经检查了所有代码,并且它与之前实现的正常工作的示例完全相同。问题是 - 每次我在auth听众中得到拒绝。
为了描述,我有两个项目:
首先 - 用于测试(我已经使用Firebase实现了auth并且它正在运行)
和第二个 - 我的主要项目(我没有成功,我试图从我的测试项目中实现相同的代码)
有我的代码
用户点击 Google LogIn 按钮,我会收到结果
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == States.GOOGLE_SIGNIN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
googleImplementation.handleSignInResult(result);
}
}
public void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
new FirebaseAuthLogIn(respond).firebaseAuthWithGoogle(acct);
}
}
public void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
String token = acct.getIdToken();
AuthCredential credential = GoogleAuthProvider.getCredential(token, null);
FirebaseAuth.getInstance().signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
------>> respond.isReject(); <-------
} else {
respond.isSuccessful();
}
}
});
}
我做错了什么?这是什么原因不起作用?在测试项目中,它在主项目中完美地工作,我一直拒绝......
如果我忘了添加一些重要的东西,请随意问我。
答案 0 :(得分:2)
Use this method which is working for me currently in my project. Make sure to enable sign-in with google in your developer console.
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("test", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
auth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("test", "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w("test", "signInWithCredential", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(MainActivity.this, OtherActivity.class));
finish();
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
// ...
}
});
}
Call that function inside handleSignInResult method
。
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
firebaseAuthWithGoogle(acct);
//mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
//updateUI(true);
}
答案 1 :(得分:2)
注意: -
请检查您在firebase控制台中的权限,以便登录google是否启用!