首页 > 解决方案 > 使用 signInAndRetrieveDataWithCredential() 进行 Firebase 身份验证 facebook 提供程序使用

问题描述

我正在尝试使用 firebase 为我的 react-native 应用程序设置 facebook 登录。我使用与 expo 集成的 facebook 支持成功地通过 facebook 进行身份验证,并获得了 facebook 令牌。然而,不幸的是,我在使用此令牌登录 firebase 时遇到问题,收到“无效的 IdP 响应/凭据”错误。

我使用完全相同的流程(但使用 Google Provider 和令牌)进行 google 登录并成功。我还能够使用 facebook 令牌从 facebook 的 rest API 获取数据,以显示令牌的有效性。

    //This function gives the error

    doSignInWithFacebookCredential = (facebookToken) => {
        const credential = this.facebookProvider.credential(facebookToken);
        this.auth.signInAndRetrieveDataWithCredential(credential)
        .then(authUser => {
            alert("It worked!")
        })
        .catch(function(error) {
            alert(error)
        });
    }



    //Code used to verify facebook token

    const response = await fetch(`https://graph.facebook.com/me?access_token=${facebookToken}`);
    console.log(await response.json())



    //This function works perfectly (The google sign-in with same flow)

    doSignInWithGoogleCredential = (googleToken) => {
        const credential = this.googleProvider.credential(googleToken);
        console.log(credential)
        this.auth.signInAndRetrieveDataWithCredential(credential)
        .then(authUser => {
            alert("It worked!")
        })
        .catch(function(error) {
            alert(error)
        });
    }

收到有效的 facebook 令牌后,会发生错误,导致 catch 发出警报,上面写着

Error: Invalid IdP response/credential: http://localhost?id_token=$facebookToken&providerId=facebook.com

在此处输入图像描述

TLDR;尽管有一个有效的 facebook 令牌,但似乎facebookProvider.credential(facebookToken)没有为signInAndRetrieveDataWithCredential()函数提供一个有效的令牌,从而导致“无效的 IdP 响应/凭据”错误。

如果有人以前使用过这些方法或有任何建议,我们将不胜感激。

标签: firebasereact-nativefirebase-authentication

解决方案


如果凭证格式错误或已过期,则抛出此错误,因此它会返回无效的 idp 响应凭证。

由于不推荐使用此方法,因此我建议改用firebase.auth.Auth.signInWithCredential。而且,也许通过这种方式,如果凭证存在问题,您将获得更多详细信息

希望这可以帮助!


推荐阅读