我有谷歌签名的工作代码。 现在,我想升级google登录库版本,因为google计划弃用当前的api。 我更新了版本和代码,但还是不行。 你能帮我吗?
我用
google_sign_in: ^6.0.0
。
我参考以下链接编写了如下所示的谷歌登录代码。
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);
}
这是我的错误。 该应用程序显示登录谷歌的对话框。 但是当我登录时,它记录了如下错误日志。
[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>
我该如何解决这个问题?
实际上@Rosenpin 或多或少是对的。
signInSilently
并不总是有效,如果浏览器中未找到会话,它将返回 null。但如果用户已登录浏览器,它将以静默方式登录。
因此,如果您使用 Chrome 浏览器并登录,则单击按钮时,您将看到
signInSilently
按预期工作。
如果您未登录浏览器,例如从 flutter 运行调试模式时,您将在浏览器上收到此日志,指示错误:
[GSI_LOGGER-TOKEN_CLIENT]: Instantiated.
[GSI_LOGGER]: No sessions found in the browser.
[GSI_LOGGER]: Prompt will not be displayed
[google_sign_in_web] Error on CredentialResponse:
[google_sign_in_web] Removing error from `userDataEvents`:
要解决此问题,您可以检查
signInSilently
是否返回 null 并且您在网络上,进行正常的登录。像这样:
GoogleSignIn googleSignIn = GoogleSignIn();
GoogleSignInAccount? googleSignInAccount =
kIsWeb ? await (googleSignIn.signInSilently()) : await (googleSignIn.signIn());
if (kIsWeb && googleSignInAccount == null) googleSignInAccount = await (googleSignIn.signIn());
希望有帮助:)
您必须先 SignInSilently 才能使用此 Flutter 包获取 IdToken,用于身份验证,然后使用 SignIn 进行范围授权
参见文档:
当用户使用 GIS SDK 时,GIS SDK 仅提供 idToken(JWT 编码信息) 成功完成身份验证流程。在插件中: 静默登录。插件登录方法使用 Oauth“隐式 Flow”来授权所请求的范围。
如果用户没有静默登录,他们必须在授权弹出流程的第一步进行登录。 如果signInSilently不成功,插件将添加额外的范围来登录并从 成功后立即通过 REST 调用 People API 授权。在这种情况下, idToken 字段 GoogleSignInUserData 将始终为空。
来自新版本的
sign_in_web
库文档:
换句话说,
signInSilently
已更改为符合图书馆无状态模式中的新 Google 标志。这意味着它总是返回 null,在我的情况下(似乎也在你的情况下)它与标准 signIn
调用冲突。
我修复此问题的方法是删除
signInSilently
调用,仅使用适当的范围调用 signIn
函数。
就您而言,如果您使用多平台应用程序,我只需将您的signInSilently 函数更改为:
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();
}
更多信息可以在这里找到:
https://pub.dev/packages/google_sign_in_web