我一直在使用google身份验证方法来获取用户身份验证,但是当我单击Google登录按钮时,它向我显示了要选择的Google电子邮件ID,但单击该按钮则无法进行其他活动。因此,从何处获取该特定令牌以进行验证或如何解决此问题。我有两个活动LoginActivity和HomeActivity,这是在LoginActivity上执行的,我想使用HomeActivity。
package com.example.firebasedemo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
public class LoginActivity extends AppCompatActivity {
EditText emailId, password;
Button btnSignIn;
TextView tvSignUp;
FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
GoogleSignInClient mGoogleSignInClient;
SignInButton signInButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mFirebaseAuth = FirebaseAuth.getInstance();
emailId = findViewById(R.id.username1);
password = findViewById(R.id.password1);
btnSignIn = findViewById(R.id.btn_signin);
tvSignUp = findViewById(R.id.txt);
signInButton=(SignInButton)findViewById(R.id.googlebutton);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent signIn=mGoogleSignInClient.getSignInIntent();
startActivityForResult(signIn,101);
Toast.makeText(LoginActivity.this,"Logging",Toast.LENGTH_SHORT).show();
}
});
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
if( mFirebaseUser != null ){
Toast.makeText(LoginActivity.this,"You are logged in",Toast.LENGTH_SHORT).show();
Intent i = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(i);
}
else{
Toast.makeText(LoginActivity.this,"Please Login",Toast.LENGTH_SHORT).show();
}
}
};
mGoogleSignInClient= GoogleSignIn.getClient(this,gso);
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailId.getText().toString();
String pwd = password.getText().toString();
if(email.isEmpty()){
emailId.setError("Please enter email id");
emailId.requestFocus();
}
else if(pwd.isEmpty()){
password.setError("Please enter your password");
password.requestFocus();
}
else if(email.isEmpty() && pwd.isEmpty()){
Toast.makeText(LoginActivity.this,"Fields Are Empty!",Toast.LENGTH_SHORT).show();
}
else if(!(email.isEmpty() && pwd.isEmpty())){
mFirebaseAuth.signInWithEmailAndPassword(email, pwd).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(LoginActivity.this,"Login Error, Please Login Again",Toast.LENGTH_SHORT).show();
}
else{
Intent intToHome = new Intent(LoginActivity.this,HomeActivity.class);
startActivity(intToHome);
}
}
});
}
else{
Toast.makeText(LoginActivity.this,"Error Occurred!",Toast.LENGTH_SHORT).show();
}
}
});
tvSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intSignUp = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intSignUp);
}
});
}
@Override
protected void onStart() {
super.onStart();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}
@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 == 101) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct)
{
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mFirebaseAuth.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
FirebaseUser user = mFirebaseAuth.getCurrentUser();
finish();
Intent intent=new Intent(getApplicationContext(),ProfileActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(),"User logged in suceessfully",Toast.LENGTH_SHORT).show();
} else {
// If sign in fails, display a message to the user.
Toast.makeText(getApplicationContext(),"Error to login",Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
}