Hello, this is my first post here, they are worthy of some inconvenience ...
Well, I'm developing an android application and I've decided to implement user authentication with Google using Firebase. Well, as I have no experience with them, I followed some tutorials on the internet and apparently everything was working. But at the time of generating the APK, that's the problem! I could not authenticate, as output, the title error message, "com.google.android.gms.common.api.ApiException: 12500:".
I searched the internet for everything and everything took me to the same place ... OAuth 2.0 client IDs
Again, I tried to follow some tutorials and the documentation itself to try to fix the bug, but I think I've gotten worse. I need your help to find out the best way to deal with my problem.
Gradient Dependencies:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.google.firebase:firebase-database:16.0.5'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.google.android.gms:play-services-ads:17.1.1'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.github.javiersantos:MaterialStyledDialogs:2.1'
implementation 'org.jetbrains:annotations-java5:15.0'
implementation 'com.google.firebase:firebase-auth:16.0.5'
}
apply plugin: 'com.google.gms.google-services'
Login Activity:
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
// Write a message to the database
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference myRef = database.getReference("users");
private Intent signInIntent;
private GoogleSignInClient mGoogleSignInClient;
GoogleApiClient mGoogleApiClient;
private FirebaseAuth mAuth;
private static final String TAG = "TESTE";
private static final int RC_SIGN_IN = 9001;
private ViewHolder mViewHolder = new ViewHolder();
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mViewHolder.sign_in = findViewById(R.id.sign_in_button);
mViewHolder.sign_in.setOnClickListener(this);
// obter a instancia do Firebase
mAuth = FirebaseAuth.getInstance();
// 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, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.i("GOOGLE API CLIENT", "NÃO DEU CERTO");
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
private void updateUI(final FirebaseUser account) {
if (account != null){
Log.i("GOOGLE SIGN IN ID:", account.getUid());
Log.i("GOOGLE SIGN IN NAME:", account.getDisplayName());
Log.i("GOOGLE SIGN IN EMAIL:", account.getEmail());
Log.i("GOOGLE SIGN IN PHOTO:", Objects.requireNonNull(account.getPhotoUrl()).toString());
myRef.child(account.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
Usuario usuario = new Usuario();
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
usuario = dataSnapshot.getValue(Usuario.class);
if (usuario == null){
// cadastrar o usuário no database
usuario = new Usuario();
usuario.setId(account.getUid());
usuario.setAdmin(false);
usuario.setEmail(account.getEmail());
usuario.setIdTurma(null);
usuario.setToken(account.getIdToken(true).toString());
usuario.setNome(account.getDisplayName());
Log.i("======== LOGIN", "BUSCANDO NO DB O USUARIO: "+ usuario.getNome());
myRef.child(usuario.getId()).setValue(usuario);
}
if (usuario.getIdTurma()!=null){
intent = new Intent(LoginActivity.this, HomeClassActivity.class);
finish();
startActivity(intent);
} else {
intent = new Intent(LoginActivity.this, IngressActivity.class);
finish();
startActivity(intent);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i("======== LOGIN", "ERRO AO BUSCAR O USUARIO: " + databaseError);
}
});
}
}
private void signIn() {
signInIntent = mGoogleSignInClient.getSignInIntent();
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) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
if (account != null) {
firebaseAuthWithGoogle(account);
}
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
Toast.makeText(LoginActivity.this, "Login com o Google falhou..."+ e, Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "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) {
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());
Snackbar.make(findViewById(R.id.sign_in_button), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
updateUI(null);
}
// ...
}
});
}
@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.sign_in_button){
// Toast.makeText(LoginActivity.this, "Button pressed...",
Toast.LENGTH_SHORT).show();
signIn();
}
else{
Toast.makeText(LoginActivity.this, "Login com o Google falhou...", Toast.LENGTH_SHORT).show();
}
}
public static class ViewHolder{
SignInButton sign_in;
}
}
Well, I do not know what might be useful for you to understand the problem ... So if there was something missing, please let me know that I put it here.