问题描述
我正在编写一个从Flutter调用google fit rest api的应用程序.
I'm writing an application that call google fit rest api from Flutter.
我需要使用( https://pub.dev/packages/google_sign_in 与Google签名..我可以毫无问题地获得令牌(请参阅),但是如何在过期后获取新令牌?
I need to sign with google using (https://pub.dev/packages/google_sign_in).I can obtain a token without problem (see Did anyone manage to get the id token from google sign in (Flutter)) but how to obtain a new token when it is expired?
我不想让用户每小时登录并获取一个新令牌
I dont' want to ask to the user to login and obtain a new token every hour
推荐答案
您可以通过两种方式进行此操作.
You can do this in 2 ways.
-
您可以使用 API .
我不知道这是否是标准设置,但是您可以在用户每次打开应用程序时执行 silent登录
,静默日志无需用户交互即可登录到用户帐户中您拥有新令牌的方式.像这样:
I don't know if this is standard but you can do a silent login
every time the user opens the app, the silent log logs into the user account without user interaction and this way you have a new token. Like this:
Future<String> refreshToken() async {
print("Token Refresh");
final GoogleSignInAccount googleSignInAccount =
await googleSignIn.signInSilently();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final AuthResult authResult = await auth.signInWithCredential(credential);
return googleSignInAuthentication.accessToken; // New refreshed token
}
这篇关于通过使用google_sign_in Flutter刷新令牌来获取新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!