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?
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() {...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With