Ich versuche, in meiner app Anmeldung mit Google zu authentifizieren.Authentifizieren mit Google Feuerbasis Authentifizierung android
Wenn ich versuche, dies zu tun, erhalte ich die FehlerGoogle Sign In failed. Status = Status{statusCode=DEVELOPER_ERROR, resolution=null}
ich einen SHA1 Schlüssel generiert haben und haben Google Auth ist in der FireBase-Konsole aktiviert.
Ich bin ein Anfänger mit der Authentifizierungsseite von Android.
public class RegisterActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {
private FirebaseAuth mFirebaseAuth;
private ProgressBar progressBar;
private ProgressDialog mProgressDialog;
private GoogleApiClient mGoogleApiClient;
private static final String TAG = "RegisterActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// Get FireBase mFirebaseAuth instance
mFirebaseAuth = FirebaseAuth.getInstance();
if (mFirebaseAuth.getCurrentUser() != null) {
Log.e("45hh", "User logged in already...");
startActivity(new Intent(RegisterActivity.this, MainActivity.class));
}
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestScopes(new Scope(Scopes.PLUS_ME))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
final EditText txtEmail = (EditText) findViewById(R.id.txtEmail);
final EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
Button btnRegister = (Button) findViewById(R.id.btnRegister);
Button btnGSignin = (Button) findViewById(R.id.btnGSignin);
TextView txtAlreadyHaveAccount = (TextView) findViewById(R.id.txtAlreadyHaveAccount);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = txtEmail.getText().toString().trim();
String password = txtPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
} else if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
// Create user
mFirebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, "Successfully registered!", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
startActivity(new Intent(RegisterActivity.this, MainActivity.class));
finish();
} else {
Toast.makeText(RegisterActivity.this, "Authentication failed. " + task.getResult(),
Toast.LENGTH_SHORT).show();
}
}
});
}
});
btnGSignin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
txtAlreadyHaveAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
}
});
}
private void handleFirebaseAuthResult(AuthResult authResult) {
if (authResult != null) {
// Welcome the user
FirebaseUser user = authResult.getUser();
Toast.makeText(this, "Welcome " + user.getEmail(), Toast.LENGTH_SHORT).show();
// Go back to the main activity
startActivity(new Intent(this, MainActivity.class));
}
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, 9001);
showProgressDialog();
}
@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 == 9001) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
if(mProgressDialog!=null){
hideProgressDialog();
}
// Google Sign In failed
Log.e(TAG, "Google Sign In failed. Status = " + result.getStatus());
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGooogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mFirebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "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(TAG, "signInWithCredential", task.getException());
Toast.makeText(RegisterActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RegisterActivity.this, "Authentication Success. Please Wait",
Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterActivity.this, MainActivity.class));
finish();
}
hideProgressDialog();
}
});
}
public void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
public void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
@Override
public void onClick(View v) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Log.d(TAG, "onConnectionFailed:" + connectionResult);
Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
}
}