Search code examples
fluttergoogle-signin

Google sign in for flutter web - exchange authorization code for refresh and access tokens


I am using google_sign_in_web 0.10.2 https://pub.dev/packages/google_sign_in_web and following the posted example

Here is some of the relevant code:

GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: [
    'email',
    'https://www.googleapis.com/auth/contacts.readonly',
  ],
);


  @override
  void initState() {
    super.initState();
            _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount? account) {
          setState(() {
            _currentUser = account;
          });
          if (_currentUser != null) {
            _handleGetContact(_currentUser!);
          }
        });
        _googleSignIn.signInSilently();
      }

I can't find a method to exchange the authorization code for refresh and access tokens

Edit: Ok, I found the access token:

Once you have the _currentUser as above you do

final authentication = await _currentUser.authentication;

print(authentication.accessToken);

I still need the refresh token


Solution

  • GoogleSignIn provides the idToken beside the accessToken.

    final GoogleSignInAuthentication googleSignInAuthentication =
            await googleSignInAccount.authentication;
    
    googleSignInAuthentication.accessToken
    googleSignInAuthentication.idToken
    

    Please refer to this answer https://stackoverflow.com/a/62230577/14729495