skip to Main Content

I have had working code to google sign.
Now, I want to upgrade google sign in library version, because of google has plan to deprecate current api.
I updated the version and code, but it’s not work.
Can you help me?


I use google_sign_in: ^6.0.0.
And I wrote google sign in code like below by referring to the following link.

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

void signInSilently() {
  _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount? account) {
    print('signInsilently log!!');
    print(account);
    if (account != null) {
      _handleGetContact(account!);
    }
  });

  _googleSignIn.signInSilently();
}

Future<void> _handleGetContact(GoogleSignInAccount user) async {
  final http.Response response = await http.get(
    Uri.parse('https://people.googleapis.com/v1/people/me/connections'
        '?requestMask.includeField=person.names'),
    headers: await user.authHeaders,
  );
  if (response.statusCode != 200) {
    print('People API ${response.statusCode} response: ${response.body}');
    return;
  }
  final Map<String, dynamic> data = json.decode(response.body) as Map<String, dynamic>;
}


Future<void> signIn() async {
  signInSilently();
  var googleSignInAccount = await _googleSignIn.signIn();
  print(googleSignInAccount);
}

And this is my error.
The application show dialog to login google.
But when i logged in, it logged error log like below.

[GSI_LOGGER-OAUTH2_CLIENT]: Checking popup closed.
[GSI_LOGGER-OAUTH2_CLIENT]: Checking popup closed.
[GSI_LOGGER-OAUTH2_CLIENT]: Checking popup closed.
[GSI_LOGGER-TOKEN_CLIENT]: Handling response. {"access_token":"ya29.a0AVvZVsrjmqr9B1aR5YtG5laxlU5DFvD8OXgxCEzFs3coO7rtPRXxekaJRYXpkD7toxNVKV0Qw40pRU28_-rjXPOYduWewbWzDSENdDV-GN4NmXdki8F1bmg4xR97Xio8G52_ojiOMn2xA0JCkH-SIWvxsCICkQaCgYKAYcSARESFQGbdwaIK-xPIdxFe4pyMbpGNhZ8XA0165","token_type":"Bearer","expires_in":3599,"scope":"email profile https://www.googleapis.com/auth/contacts.readonly https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid","authuser":"0","prompt":"none"}
[GSI_LOGGER-OAUTH2_CLIENT]: Popup timer stopped.
[GSI_LOGGER-TOKEN_CLIENT]: Trying to set gapi client token.
[GSI_LOGGER-TOKEN_CLIENT]: The OAuth token was not passed to gapi.client, since the gapi.client library is not loaded in your page.
Error: {
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49  throw_
packages/google_sign_in_web/src/people.dart 146:7                                                                          _doRequest
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50            <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1653:54                                          runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 147:18                                    handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 766:44                                    handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 795:13                                    _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 566:5                                     [_completeWithValue]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 639:7                                     callback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 40:11                              _microtaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 49:5                               _startMicrotaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 166:15           <fn>

How can I fix this?

2

Answers


  1. You have to SignInSilently before to get an IdToken with this Flutter package, for authentication, then you use SignIn for Authorization of the scopes

    See documentation :

    The GIS SDK only provides an idToken (JWT-encoded info) when the user
    successfully completes an authentication flow. In the plugin:
    signInSilently. The plugin signIn method uses the Oauth "Implicit
    Flow" to Authorize the requested scopes.

    If the user hasn’t signInSilently, they’ll have to sign in as a first step of the Authorization popup flow.
    If signInSilently was unsuccessful, the plugin will add extra scopes to signIn and retrieve basic Profile information from the
    People API via a REST call immediately after a successful
    authorization. In this case, the idToken field of the
    GoogleSignInUserData will always be null.

    Login or Signup to reply.
  2. From the new version of sign_in_web library documentation:
    documentation relating to issues with signInSilently

    In other words, signInSilently was changed to conform to the new Google sign in library stateless mode. This means that it will always return null and in my case (seems like in yours too) it conflicts with the standard signIn call.

    What I did to fix it was remove the signInSilently call and only call the signIn function with the appropriate scopes.

    In your case, if you work on a multiplatform app, I’d just change your signInSilently function to:

    void signInSilently() {
      _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount? account) {
        print('signInsilently log!!');
        print(account);
        if (account != null) {
          _handleGetContact(account!);
        }
      });
    
      if (kIsWeb) {
          print('skipping silent login on web');
          return;
      }
      _googleSignIn.signInSilently();
    }
    

    More information can be found here:
    https://pub.dev/packages/google_sign_in_web

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search