I am trying to integrate Google sign-in into my android app using Firebase, but I am running into issues. I am following the tutorial here, and I believe that I have followed it to the word. I added Firebase to my app, added my SHA-1 fingerprint, enabled Google sign-in, and added the dependencies to my grade files. Then, I copied the code in the github project linked to in the tutorial. However, when I run the application, and my code is below, I get an error when the sign-in fragment returns, and the result code is RESULT_CANCELED
. This is the error output:
W/GoogleSignInActivity: Google sign in failed, resultCode: 0
com.google.android.gms.common.api.ApiException: 10:
at com.google.android.gms.common.internal.zzb.zzy(Unknown Source:14)
at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source:37)
at com.example.root.firebasesignin.LoginActivity.onActivityResult(LoginActivity.java:63)
at android.app.Activity.dispatchActivityResult(Activity.java:7267)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4524)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4571)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
When I created the SHA-1 fingerprint, I did have to remake the debug keystore at ~/.android/debug.keystore
using the command
keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"
and then I converted the keystore to pkcs12 using the command
keytool -importkeystore -srckeystore ~/.android/debug.keystore -destkeystore ~/.android/debug.keystore -deststoretype pkcs12
I have never used Firebase or Google sign-in for authentication, so I am very lost. I think the keystore might be the problem, and when I look in File > Project Structure > Signings
nothing is shown in the left panel, and in File > Project Structure > Build Types
the Signing Config box is empty. Again, I am very new to Firebase and Google authentication, so please excuse me if I'm forgetting something simple. Thank you in advance for your help.
This is the code for my main activity. The layout is simply a Google sign-in button and the action bar.
package com.example.root.firebasesignin;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
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.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 implements View.OnClickListener {
private static final String TAG = "GoogleSignInActivity";
private static final int RC_SIGN_IN = 9001;
private FirebaseAuth mAuth;
private GoogleSignInClient mGoogleSignInClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Button listeners
findViewById(R.id.sign_in_button).setOnClickListener(this);
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
mAuth = FirebaseAuth.getInstance();
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
@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, "Google sign in failed, resultCode: " + resultCode, e);
updateUI(null);
}
}
}
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());
Toast.makeText(LoginActivity.this, "Authentiation failed", Toast.LENGTH_LONG).show();
updateUI(null);
}
}
});
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void updateUI(FirebaseUser user) {
if (user != null)
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
else
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.sign_in_button)
signIn();
}
}
See Question&Answers more detail:
os