Flutter Google Photo API 随机失败,我尝试注销并登录多次。
登录流程工作正常,但每当我调用谷歌照片的 API 时,它总是失败:
Unhandled Exception: Access was denied (www-authenticate header was: Bearer realm="https://accounts.google.com/", error="invalid_token").
我花了很长时间才解决这个问题。事实证明,一行代码就解决了。
await user?.clearAuthCache();
有关更多上下文,这是错误 401,在文档的深处,我发现了
/// Clears any client side cache that might be holding invalid tokens.
///
/// If client runs into 401 errors using a token, it is expected to call
/// this method and grab `authHeaders` once again.
Future<void> clearAuthCache() async
非常令人沮丧,我希望它自动刷新令牌而不在我的所有呼叫中监听此状态代码,我使用它并要求用户手动执行此操作:
Future<void> handleSignOut() async {
try {
final user = await getUser();
user?.clearAuthCache(); // added this line of code
} catch (e) {
print(e);
}
await _googleSignIn.disconnect();
client = null;
currentUser = null;
}