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
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 :
From the new version of
sign_in_web
library documentation: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 standardsignIn
call.What I did to fix it was remove the
signInSilently
call and only call thesignIn
function with the appropriate scopes.In your case, if you work on a multiplatform app, I’d just change your signInSilently function to:
More information can be found here:
https://pub.dev/packages/google_sign_in_web