这是通过Google项目登录。 我已经连接到Firebase并在那里设置了项目。是的,我已经启用了Google登录方法。
“登录”按钮有效-但这与Toast消息“身份验证失败”
我读了几个小时的代码后可能会过时,这让我很疑惑(我是菜鸟)。
我的登录按钮也有一些奇怪的错误(不影响设备的构建)。
MainActivity.java
SignInButton button;
FirebaseAuth mAuth;
private final static int RC_SIGN_IN = 2;
GoogleApiClient mGoogleApiClient;
FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (SignInButton) findViewById(R.id.googlebtn);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null){
startActivity(new Intent(MainActivity.this, SecondOne.class));
}
}
};
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken( getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(MainActivity.this,"Something went wrong", Toast.LENGTH_SHORT).show();
}
} )
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@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 succesfull, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
FirebaseAuthWithGoogle(account);
} else {
// Google Sign In Failed, update UI appropriatly
//..
Toast.makeText(MainActivity.this,"Auth went wrong", Toast.LENGTH_SHORT).show();
}
}
}
private void FirebaseAuthWithGoogle(GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("TAG", "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
//updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w("TAG", "signInWithCredential:failure", task.getException());
Toast.makeText(MainActivity.this,"Authentication Failed.", Toast.LENGTH_SHORT).show();
// updateUI(null);
}
}
});
}
}