Firebase and new Google Sign-In on Android

10,049

It's a mix of the steps in Add Sign-In to Android and Authorizing with Google for REST APIs.

Once you have a GoogleSignInResult you can get the account name from that and then request the token with the minimal scopes:

@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) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        GoogleSignInAccount acct = result.getSignInAccount();

        String email = acct.getEmail();

        // TODO: run an async task to get an OAuth2 token for the account
    }
}

The async task will need to request these scopes:

protected String doInBackground(String... params) {
    String scopes = "oauth2:profile email";
    String token = GoogleAuthUtil.getToken(getApplicationContext(), email, scopes);
    // exception handling removed for brevity
    return token;
}

Now you can use the token to sign in to Firebase as usual:

ref.authWithOAuthToken("google", token, new Firebase.AuthResultHandler() {...
Share:
10,049
Bogdan M.
Author by

Bogdan M.

Updated on July 03, 2022

Comments

  • Bogdan M.
    Bogdan M. about 2 years

    I'm trying to add support for the new Google Sign-in announced as part of Play services 8.3.0. I successfully configured the project and I'm getting a token from the GoogleApiClient, but Firebase is returning an Invalid Credentials error when calling ref.authWithOAuthToken("google", token)

    Google+ sign-in is working but that requires a separate permission which is a pain when developing for Marshmallow. Firebase android tutorial has a Google+ sign-in sample and my feeling is that they dont' have support for the new Google Sign-In yet.

    Has anyone tried the new Google Sign-In in connection with Firebase and got it to work?

  • carloshwa
    carloshwa over 8 years
    Thanks for figuring that out! I created a pull request to update the Firebase login demo to work without Google+: github.com/firebase/firebase-login-demo-android/pull/21
  • Steffen
    Steffen over 8 years
    I'm getting a BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.google.android.gms.auth.TokenDat at GoogleAuthUtil.getToken(...). What could be wrong?
  • Steffen
    Steffen over 8 years
    Oh, it was just a ProGuard/DexGuard problem. -keep public class com.google.android.gms.auth.** { public *; } did help.
  • Frank van Puffelen
    Frank van Puffelen over 8 years
    Ah, that explains. Might be worth asking as a separate question and self-answering, so that others can benefit from your experience.
  • cybergen
    cybergen over 8 years
    Great answer! You're an angel @BogdanM.
  • DariusL
    DariusL over 8 years
    getToken with accountName is now deprecated. Any other solutions?
  • Creos
    Creos over 8 years
    This is a great solution but I find the only way to get it to work is to use "oauth2:" + Scopes.PLUS_LOGIN which generates a much more "scary" permissions request popup. Any ideas why the plain "oauth2:profile email" do not work?
  • Frank van Puffelen
    Frank van Puffelen over 8 years
    The snippet I posted doesn't use PLUS_LOGIN and works. If you're having problems, post a new question that includes a minimum, complete verifiable example of what you've done.
  • Creos
    Creos over 8 years
    thanks Frank, i think there was some issue with some cached state on my device and the account. after i cleared all perms on the account and cleared app cache, your solutions started working. thx
  • Matthew Hooker
    Matthew Hooker over 7 years