我已经完成了几乎所有的firebase google身份验证问题,但我仍无法找到问题的解决方案。
以下是我的Firebase Google身份验证代码
在 onCreate
内 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mAuth = FirebaseAuth.getInstance();
findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
MethodToSignIn
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
onActivityResult
@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) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed, update UI appropriately
// ...
}
}
}
FirebaseAuthWithGoogle
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("MainACtivity", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("Main", "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("MainAcitivyt", "signInWithCredential", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
activity_welcome.xml
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_below="@+id/buttonSendData"
android:layout_height="wrap_content" />
当我点击按钮时,我在logcat中获得以下内容并且应用程序冻结
D/ActivityThreadInjector: clearCachedDrawables.
V/FA: Using measurement service
V/FA: Connection attempt already in progress
V/FA: Using measurement service
V/FA: Connection attempt already in progress
V/FA: Activity resumed, time: 285185857
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 3
V/FA: Inactivity, disconnecting from the service
V/FA: Connecting to remote service
V/FA: Activity paused, time: 285382455
D/FA: Logging event (FE): _e, Bundle[{_o=auto, _et=10085, _sc=SignInHubActivity, _si=3238861053606655861}]
V/FA: Using measurement service
V/FA: Connection attempt already in progress
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 2
V/FA: Inactivity, disconnecting from the service
答案 0 :(得分:2)
从您的项目设置中指定您应用的SHA-1指纹 firebase控制台。在登录方法中启用谷歌登录。也尝试最新的依赖项。