gpt4 book ai didi

firebase - 使用 signInWithCredential() 进行身份验证

转载 作者:行者123 更新时间:2023-12-04 18:02:57 30 4
gpt4 key购买 nike

我正在尝试连接到第二个 Firebase 应用并使用 signInWithCredential() 进行身份验证,但我不知道如何为第二个应用获取有效的 idToken :

  connect(accessToken: string, config: FirebaseAppConfig) {
let one: firebase.app.App = this.angularFireTwo.database["fbApp"];
one.auth().currentUser.getToken()
.then(idToken => firebase.auth.GoogleAuthProvider.credential(idToken, accessToken))
.then(credential => {
let two = firebase.initializeApp(config, `[${config.apiKey}]`);
return two.auth().signInWithCredential(credential);
})
.catch(console.warn)
.then(console.info);
}

我从 https://www.googleapis.com/identitytoolkit/v3/ 得到错误:

Invalid id_token in IdP response



如果我使用 signInWithPopup() 我可以进行身份​​验证并且连接正常工作:
        two.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider())

任何人都知道我应该怎么做才能获得有效的 idToken?

更新:

我一直在试图弄清楚身份验证过程,据我所知,它是这样的:

来自 config: FirebaseAppConfig firebase 的
  • 读取 apiKeyauthDomain
  • 它联系服务器并获取 Web 客户端 ID 用于启用的 Google 提供程序 123.apps.googleusercontent.com
  • 使用这个 Web Client IDauthDomain 联系 www.googleapis.com,返回 idToken
  • idToken 然后用于识别请求用户访问用户个人资料等权限的应用程序。
  • 当用户同意时,回调返回用户详细信息+用于此身份验证的凭据,其中包含 web 应用程序的 idToken 和用户的 accessToken

  • 现在,如果我使用 signInWithPopup() 步骤 2-3-4 在后台完成(弹出窗口)。我只需要一种为步骤 4 生成 idToken 的方法,因此我可以使用它来生成凭证 firebase.auth.GoogleAuthProvider.credential(idToken, accessToken) 并使用 signInWithCredential() 登录。

    我可以访问登录第二个应用程序所需的一切 - 它是; apiKeyauthDomain 、Web 客户端 ID 456.apps.googleusercontent.com 和用户唯一的 accessToken

    但是还是想不通怎么做。我尝试将应用程序的 一个 两个 Web 客户端 ID 在其身份验证配置中列入白名单,希望这能让他们相互接受 idToken s,但这并没有

    最佳答案

    你打电话时:
    firebase.auth.GoogleAuthProvider.credential(idToken, accessToken))

    第一个参数应该是 Google OAuth Id token 。您正在使用 Firebase Id token ,这就是您收到错误的原因。此外,如果您已经登录,为什么还要使用signInWithCredential 再次登录?

    如果您需要使用 Google 凭据登录,则需要 Google OAuth Id token 或 Google OAuth 访问 token 。

    要将 Firebase OAuth 登录状态从一个应用程序复制到另一个应用程序,您可以从 signInWithPopup 结果中获取凭据,并在第二个实例中使用它来 signInWithCredential。
    two.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider())
    .then(function(result) {
    return one.auth().signInWithCredential(result.credential);
    });

    关于firebase - 使用 signInWithCredential() 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40064744/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com