【问题标题】:How to get the Firebase ID token from Google oAuth token如何从 Google oAuth 令牌获取 Firebase ID 令牌
【发布时间】:2021-09-07 19:07:11
【问题描述】:

我正在构建一个执行 auth.SignInWithCredential({'accessToken'...,'idToken'...}) 的 Flutter 应用程序

我希望应用程序执行后端请求,发送一个不记名令牌。 在后端,我需要使用 firebase 对该用户进行身份验证,并将 oAuth2 令牌用于 Google 服务,因为它是有范围的。

我没有这样做的聪明主意(除了在请求标头中发送 2 个令牌,这似乎有点矫枉过正)。

【问题讨论】:

  • 我的最终解决方案是仅将 Google-token 发送到后端,并使用 signInWithIdp 生成 Firebase ID 令牌。这适用于任何服务器。

标签: node.js firebase flutter google-authentication


【解决方案1】:

我的最终解决方案是仅将 Google 令牌发送到后端,并使用 signInWithIdp 生成 Firebase ID 令牌。这适用于任何服务器。

    const response = await fetch('https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key=[firebase_web_key]', 
    { 'method': 'POST', 
    body: JSON.stringify({
      postBody: "access_token="+req.token+"&providerId=google.com",
      requestUri: "https://[firebase-project].firebaseapp.com",
      returnIdpCredential: true,
      returnSecureToken: true,
    }) })

    // contains idToken!
    const idpData = await response.json()

    res.locals.decodedIdToken = await admin.auth().verifyIdToken(idpData.idToken);
    ```

【讨论】:

    猜你喜欢
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多