GoogleAuthProvider
我在 Vue 2 应用程序中使用 Firebase来登录用户。我将日历添加到提供程序的范围中,以便访问用户的日历。然后,我使用结果中给出的 access_token 来检索事件。问题是accessToken
一小时后就会过期。我怎样才能更新它?
这是我处理登录的代码:
const auth = getAuth();
const provider = new GoogleAuthProvider();
provider.addScope("https://www.googleapis.com/auth/calendar");
const result = await signInWithPopup(auth, provider);
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
const user = result.user;
await this.checkUser(user, token);
Run Code Online (Sandbox Code Playgroud) javascript google-authentication access-token oauth-2.0 firebase
我有一个 Firebase 应用程序,可以对用户进行身份验证并返回一个访问令牌,然后我可以使用该令牌来访问 Google Calendar 和 Sheets API。我还保存了刷新令牌。经过身份验证的令牌的示例代码:
firebase
.signInWithGoogle()
.then(async (socialAuthUser) => {
let accessToken = socialAuthUser.credential.accessToken // token to access Google Sheets API
let refreshToken = socialAuthUser.user.refreshToken
this.setState({accessToken, refreshToken})
})
Run Code Online (Sandbox Code Playgroud)
1 小时后,accessToken 将过期。Firebase 身份验证在登录后在用户对象上提供刷新令牌
我使用该刷新令牌重新进行身份验证并通过发布到以下位置获取新的 access_token:
https://securetoken.googleapis.com/v1/token?key=firebaseAppAPIKey
新的访问令牌不再适用于 Google API,并且不再具有授权范围。我也尝试将其发送到
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token="refreshToken"
它给我错误“无效令牌”。当我使用 firebase 的原始令牌时,它工作得很好。
还有其他人遇到类似的问题吗?我还没有找到一种方法来使用正确的访问范围刷新原始访问令牌,而不需要用户注销并再次登录。
谢谢!
google-calendar-api google-api firebase google-drive-api google-oauth