如何在身份令牌过期之前使用 Flutter google_sign_in 插件为 android 刷新身份令牌

Zil*_*mil 8 android google-signin flutter

我正在使用 google_sign_in flutter 插件将 Google Sign In Auth 与我的应用程序集成。除了 idToken 在一小时后过期外,它一切正常。就在它到期之前,我正在调用 signInSilently 来刷新令牌。它适用于 iOS,但对于 Android,它返回相同的旧令牌。我查看了插件的代码,根据对代码的评论,它看起来像是设计使然,它不会刷新 Android 的令牌。

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);
  }
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果是这种情况,我该如何刷新令牌(尤其是对于 android)。在插件回购,我还发现了其他开发者抱怨同样的问题,但没有任何反应出现

总的来说,我的目标是让用户即使在一个月后也不需要每次打开应用程序时都登录。谢谢

小智 0

尝试下面的代码,我可以在每次完美运行时打开应用程序而无需签名,

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);
Run Code Online (Sandbox Code Playgroud)