我正在使用 google_sign_in flutter 插件将 Google 登录身份验证与我的应用程序集成。除了 idToken 一小时后过期外,一切正常。就在它过期之前,我正在调用 signInSilently 来刷新 token 。它适用于 iOS,但对于 Android,它返回相同的旧 token 。我查看了插件的代码并根据对代码的评论,它看起来像是设计的,它不会刷新 Android 的 token 。
Future<GoogleSignInAuthentication> get authentication async {
if (_googleSignIn.currentUser != this) {
throw StateError('User is no longer signed in.');
}
final GoogleSignInTokenData response =
await GoogleSignInPlatform.instance.getTokens(
email: email,
shouldRecoverAuth: true,
);
// On Android, there isn't an API for refreshing the idToken, so re-use
// the one we obtained on login.
if (response.idToken == null) {
response.idToken = _idToken;
}
return GoogleSignInAuthentication._(response);
}
我的问题是,如果是这种情况,我该如何刷新 token (尤其是对于 android)。在插件仓库中,我还发现其他开发人员提示同样的问题,但没有回应 there .总的来说,我的目标是让用户即使在一个月后也不需要每次打开应用程序时都登录。
谢谢
最佳答案
试试下面的代码,我可以在每次完美运行时打开应用程序而无需签名,
const _scopes = const [cal.CalendarApi.CalendarScope];
//I'm using google sign in for the purpose of the calendar so I'm using
//this scope
final _googleSignIn = new GoogleSignIn(scopes: _scopes);
try {
await _googleSignIn.isSignedIn().then((value) async {
print("silently");
await _googleSignIn.signInSilently();
});
} catch (e) {
await _googleSignIn.signIn();
}
final Map<String, dynamic> authHeaders =
await _googleSignIn.currentUser.authHeaders;
//you can now use this new token for your purpose
_googleSignIn.currentUser.authentication.then((value) => value.idToken);
关于android - 如何使用适用于 android 的 flutter google_sign_in 插件在身份 token 过期之前刷新身份 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66002871/