Firebase and new Google Sign-In on Android
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() {...
Bogdan M.
Updated on July 03, 2022Comments
-
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 callingref.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 over 8 yearsThanks 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 over 8 yearsI'm getting a
BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.google.android.gms.auth.TokenDat
atGoogleAuthUtil.getToken(...)
. What could be wrong? -
Steffen over 8 yearsOh, it was just a ProGuard/DexGuard problem.
-keep public class com.google.android.gms.auth.** { public *; }
did help. -
Frank van Puffelen over 8 yearsAh, that explains. Might be worth asking as a separate question and self-answering, so that others can benefit from your experience.
-
cybergen over 8 yearsGreat answer! You're an angel @BogdanM.
-
DariusL over 8 years
getToken
withaccountName
is now deprecated. Any other solutions? -
Creos over 8 yearsThis 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 over 8 yearsThe 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 over 8 yearsthanks 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 over 7 years