Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase and new Google Sign-In on Android

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?

like image 870
Bogdan M. Avatar asked Nov 10 '15 14:11

Bogdan M.


1 Answers

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() {...
like image 50
Frank van Puffelen Avatar answered Nov 14 '22 08:11

Frank van Puffelen