android - 如何使用适用于 android 的 flutter google_sign_in 插件在身份 token 过期之前刷新身份 token

标签 android flutter google-signin

我正在使用 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/

相关文章:

flutter - 如何检测带有 flutter/火焰的游戏中的滑动

安卓 Youtube 认证登录 SDK

ios - 使用 Google Sign In for iOS 时未捕获 NSInvalidArgumentException

java - ListView setAdapter() 空指针异常(使用 Inflator)

java - HashMap 方法将其用作最常见的值

java - 关于上下文 FLAG_ACTIVITY_NEW_TASK 的错误

Android Firebase 身份验证 : federated Google login not working in Oreo

java - Android:启动新 Activity 时关闭 Activity ?

flutter - 使软键盘与其他小部件重叠 - Flutter

database - flutter 中使用 sqflite 保存嵌套模型的有效方法