Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OAuth2 issues after upgrading firebase

I have a working android app (Maybe better say HAD).

The app is using some third party libraries including: Google maps, Firebase, firebaseui. After converting the app to use the new firebase I have stumbled upon two main issues:

  1. The firebaseui logon screen changed and now looks like this (from login dialog it turned into activity):

sign in screenshot

which is of course completely different from the old one and for some odd reason different from the debug build variant.

  1. I am unable to use the google sign in with the following error showing in logcat:

com.google.firebase.FirebaseException: An internal error has occured. [ OAuth2 client id in server configuration is not found. ]

Again this issue is not happening in the debug variant.

Just to be clear the debug build variant uses the same code base but a different firebase database.

like image 989
CaptainNemo Avatar asked Jun 11 '16 22:06

CaptainNemo


2 Answers

For the following error:

com.google.firebase.FirebaseException: An internal error has occured. [ OAuth2 client id in server configuration is not found. ]

  1. You have to first go to your Google developer console in the credential category (https://console.developers.google.com/apis/credentials) then find the auto generated OAuth 2.0 client ID named something like "Web client (auto created by Google Service)", keep data related to this client ID.

  2. Go to Firebase console in the "Auth" category, then go to "Connection mode" tab, open Google authentication settings, and finally expand the Web SDK settings. Now paste the previously found OAuth client Id with its key and save settings.

The changed setting is about Web settings but it seem to be used by Firebase android component.

like image 144
quent Avatar answered Sep 27 '22 18:09

quent


With New Firebase, they have changed a whole lot of Code and the way of doing things.

Now when you setup your Firebase Project, they give you an Option to enter your AuthId, you can download the JSON and paste it inside your app.

After you setup the ClientId, you can add this code to get your Google Login back to working again.

 Button signInButton;
 //Google Variables
 private static final int RC_SIGN_IN = 9001;
 private GoogleApiClient mGoogleApiClient;

 //Firebase Variables
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;

//Inside OnCreate();
//Google SignIn Methods
    // [START config_signin]
    // Configure Google Sign In
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
    // [END config_signin]

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();


    mAuth = FirebaseAuth.getInstance();

    mAuthStateListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            updateUI(user);
        }
    };

 @Override
protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthStateListener);
    mGoogleApiClient.connect();
}


@Override
protected void onStop() {
    super.onStop();
    if(mAuthStateListener != null) {
        mAuth.removeAuthStateListener(mAuthStateListener);
    }
    mGoogleApiClient.disconnect();
}

// [START onactivityresult]
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);
        } else {
            // Google Sign In failed, update UI appropriately
            // [START_EXCLUDE]
            updateUI(null);
            // [END_EXCLUDE]
        }
    }

}
// [END onactivityresult]


private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    // 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()) {
                        Toast.makeText(LoginActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });
}
// [END auth_with_google]

// [START signin]
private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signin]

   private void updateUI(FirebaseUser user) {
    if (user != null) {
      //write what you want to do after login here  
    }
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    // An unresolvable error has occurred and Google APIs (including Sign-In) will not
    // be available.
    Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
}

 public void GoogleSignIn(View v){
    signIn();
}


@Override
protected void onRestart() {
    super.onRestart();
    mGoogleApiClient.connect();
}

This Worked Completely fine for Google Authentication using Firebase in my app

like image 22
Siddhesh Dighe Avatar answered Sep 27 '22 16:09

Siddhesh Dighe