尝试将FB用户添加到Firebase身份验证列表中时出现“错误:IdP响应无效/凭据无效”

时间:2019-12-22 03:35:00

标签: javascript reactjs firebase-authentication expo facebook-authentication

我一直在努力在基于Expo的应用程序中实现Facebook和Google登录方法。

在将Firebase与我的应用程序连接后,我希望在Firebase用户中列出该用户。虽然Google登录似乎可以正常运行,但Facebook从firebase.auth().signInWithCredential(credential)发出了如下错误:

[Error: Invalid IdP response/credential: http://localhost?id_token=EAAGUBzIZAgb0BABAkdrj5NcUcvljcsLeNBQlV5VUZAZC7M8e7sSRg2MkqlFCuD7tKjin4uZBep5gSs20oAo8fXKiUqq2deEbUl6HoaAUskTda7x49VCqqcbYh1W3566fMZBtRFB5S3fRV7D41AGVGPMAck91l1KiFCzQzCGtSf5g6ZBKoyHw03LOVONcOiwVZB4vXVcGPYmIzL3RuzzztdBNLRql5ndSk0ZD&providerId=facebook.com] 

这是代码的相关部分

  1. Firebase.js
// imports, configs and exports omitted
export const facebookProvider = new firebase.auth.FacebookAuthProvider;
  1. LoadingScreen.js(登录部分)
import firebase, { googleProvider, facebookProvider } from "../firebase";
import * as Facebook from "expo-facebook";

const LoadingScreen = ({ navigation }) => {
  const signInWithFacebook = async () => {
    try {
      await Facebook.initializeAsync("444233602924989");
      const { type, token } = await Facebook.logInWithReadPermissionsAsync(
        "444233602924989",
        {
          permissions: ["public_profile", "email"]
        }
      );
      if (type === "success") {
        const credential = facebookProvider.credential(token);
        onSignInFB(credential);
      } else {
        Alert.alert('FB sign-in failed')
      }
    } catch ({ message }) {
      console.log(`Facebook 1 Error: ${message}`);
      Alert.alert(`first ${message}`);
    }
  };

  const onSignInFB = credential => {
    firebase
      .auth()
      .signInWithCredential(credential)
      .then(function() {
        navigation.navigate("Home");
      })
      .catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
      });
  };
};

我记录了一些元素,如下所示:

  • 尽管没有错误属性可用,但错误代码为auth/internal-error。 Console.logging错误本身会产生错误Invalid IdP response/credential
  • 来自logInWithReadPermissionsAsync的令牌是有效的访问令牌(字符串)
  • credential中的facebookProvider.credential(token)是:
    Object {"oauthIdToken": "EAAGUBzIZAgb0BAKgeMa5E7qHm8WNJYv5SeuQ8DHyhkUlAnkMhE7niu6tx3e2amSMHSEqG9B0MV4a9dygwgjs337PR7AA3M4PZB2F6x6n1FwAEyZBKhZBpOSE2OWQ9dJipirpafg61TKX36hnKIzaIcwRkjs8YYRBbDuLnZAhJzWst3ZBM5tafwxYKumv2F4kYdexxZAXqb1nosnwYodNvB9bstkcaBrfB8ZD",
      "providerId": "facebook.com",
      "signInMethod": "facebook.com",
    }
  • firebase.auth().currentUsernull

我还仔细检查了以下可能出现的错误,但找不到:

  • 在Firebase身份验证登录中启用了功能块
  • AppId和App Secret已正确从FB应用复制到Firebase(以及重定向URI)
  • 将访问令牌从Facebook.logInWithReadPermissionsAsync传递到Facebook的访问令牌调试器,看看是否有任何错误(例如过期或无效)
  • 检查是否从facebookProvider.credential(token)返回了实际凭证并将其传递给signInWithCredential

找出问题的根源真是太好了! (一种可能是从logInWithReadPermissionsAsync返回的访问令牌与oauthIdToken对象中的credential属性相同,但是我没有手动放置或更改其中任何一个。) / p>

感谢您的阅读:)

1 个答案:

答案 0 :(得分:3)

我今天对此表示震惊,解决方案是将对象传递给credential调用,即使文档说要传递字符串也是如此。另外,对象属性需要键入为accessToken

所以代替: const credential = facebookProvider.credential(token);

尝试: const credential = facebookProvider.credential({ accessToken: token });

基于文档,我不知道为什么需要该键或为什么它必须是一个对象,但是在我的情况下它可以工作。希望对您有帮助!