I'm developing an Android App with Google access with Firebase. I've got an Activity to handle the login, and it worked since yesterday: after checking login and logout the first time, I haven't checked them anymore. Now I pushed the logout button and I get this Exception when I try to login:
W: com.google.android.gms.common.api.ApiException: 10:
com.google.android.gms.common.api.ApiException: 10:
at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(com.google.android.gms:play-services-base@@17.1.0:4)
at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source:8)
at com.example.locker.LoginActivity.onActivityResult(LoginActivity.java:92)
at android.app.Activity.dispatchActivityResult(Activity.java:8110)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4838)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4886)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I've tried to put the sha1 fingerprint with firebase and Android Studio again, and to delete and re-insert my Google account, but nothing changes.
Here's my Activity:
public class LoginActivity extends Activity implements View.OnClickListener{
private static final String TAG = LoginActivity.class.getSimpleName();
private static final int RC_SIGN_IN = 9001;
private Button login_button;
private ProgressBar progress_login;
private ImageView welcome_logo;
private FirebaseAuth mAuth;
private FirebaseUser user;
private GoogleSignInClient mGoogleSignInClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
login_button = findViewById(R.id.login_button);
progress_login = findViewById(R.id.progress_login);
welcome_logo = findViewById(R.id.welcome_logo);
login_button.setOnClickListener(this);
// Initialize Firebase Auth
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();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
// Logout check
if (intent.hasExtra("logout")
&& intent.getBooleanExtra("logout", true)) signOut();
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
user = mAuth.getCurrentUser();
updateUI();
}
@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);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, e.toString(), e);
// [START_EXCLUDE]
user = null;
updateUI();
// [END_EXCLUDE]
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
user = mAuth.getCurrentUser();
updateUI();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
user = null;
updateUI();
}
});
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
public void signOut() {
// Firebase sign out
mAuth.signOut();
user = null;
// Google sign out
mGoogleSignInClient.signOut().addOnCompleteListener(this,
task -> updateUI());
}
private void revokeAccess() {
// Firebase sign out
mAuth.signOut();
user = null;
// Google revoke access
mGoogleSignInClient.revokeAccess().addOnCompleteListener(this,
task -> updateUI());
}
private void updateUI() {
if (user != null) {
Intent intent = new Intent(LoginActivity.this, LockerActivity.class);
//Passing user data
intent.putExtra("email", user.getEmail());
intent.putExtra("name", user.getDisplayName());
intent.putExtra("userID", user.getUid());
startActivity(intent);
}
}
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.login_button) {
progress_login.setVisibility(View.VISIBLE);
signIn();
progress_login.setVisibility(View.INVISIBLE);
}
}
}
And my logout (where I can't go anymore obviously) in the second Activity:
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.logout_button) {
Intent intent = new Intent(LockerActivity.this, LoginActivity.class);
intent.putExtra("logout", true);
startActivity(intent);
}
}
I really don't know how to fix this error, expecially because I didn't edit this code since when I wrote and tested it.
Thank you!