Firebase and new Google Sign-In on Android

后端 未结 2 875
死守一世寂寞
死守一世寂寞 2020-12-08 05:35

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 Goog

2条回答
  •  余生分开走
    2020-12-08 06:04

    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() {...
    

提交回复
热议问题