firebase - 使用 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 响应/凭据”错误。
如果有人以前使用过这些方法或有任何建议,我们将不胜感激。
解决方案
如果凭证格式错误或已过期,则抛出此错误,因此它会返回无效的 idp 响应凭证。
由于不推荐使用此方法,因此我建议改用firebase.auth.Auth.signInWithCredential。而且,也许通过这种方式,如果凭证存在问题,您将获得更多详细信息
希望这可以帮助!
推荐阅读
- python - 对加权散点图使用颜色强度而不是气泡大小
- timezone-offset - 计算仅知道当前偏移量的未来日期的时区偏移量
- php - 数据库异常在 Mac / Unix 系统上从 PHP 连接到 MSSQL 数据库时找不到驱动程序
- javascript - 如何为渲染和逻辑重构反应组件?
- javascript - 反应数组中的子组件 - 将元素移动到其他索引时出现问题
- latex - LaTeXt PDF文件打不开
- reactjs - React-leaflet v3.1.0 中的搜索框实现给出错误“TypeError: Object(...) is not a function”
- python - 获取两个 geopandas 数据框几何点之间的距离
- r - 读取 csv 文件的特定行及其以下转换
- python - 创建自定义数组