ID Token [host.exp.Exponent] 中的受众与预期受众不匹配

问题描述 投票:0回答:1

我收到错误“[FirebaseError:Firebase:ID 令牌 [host.exp.Exponent] 中的受众与预期受众不匹配。(auth/invalid-credential)。]”

我目前正在尝试在我的 expo 应用程序上设置“使用 Apple 登录”,并将其连接到 Firebase 进行用户身份验证。我在苹果开发者帐户中创建的服务 ID 和 Firebase 上 Apple 登录方法的服务 ID 是相同的。

我想如果我知道错误消息从哪里获取 [host.exp.Exponent],我可以很容易地解决这个问题。我的 app.json 中的捆绑包 ID 与我的苹果开发者帐户中的应用程序 ID(与服务 ID 不同)相同。

我已经查看了存在相同错误的其他问题,但没有一个解决方案对我有用(服务 ID 与苹果开发者帐户和 firebase 匹配,app.json 中的捆绑包 ID 与开发者帐户中的应用程序 id 匹配)

firebase react-native expo apple-authentication
1个回答
0
投票
问题的范围很笼统,没有太多信息可供参考,让我尝试以我有限的知识和理解给出一些建议,谁知道可能会有所帮助。

现在让我们看看,

[FirebaseError:Firebase:ID 令牌 [host.exp.Exponent] 中的受众与预期受众不匹配。 (授权/无效凭证)。]

首先,从错误来看,只能假设它与 ID 令牌中的受众(客户端 ID)与 Firebase 期望的不匹配有关。


现在让我们看看 [host.exp.Exponent] 受众是什么?

此类受众通常与世博发展环境相关。当您在 Expo Go 客户端或开发模式下运行应用程序时使用它。

Firebase 有何期望?

Firebase 希望受众与您在 Firebase 控制台中为应用设置的 OAuth 客户端 ID 相匹配。


如果我知道错误消息是从哪里获取 [host.exp.Exponent],我想我可以很容易地解决这个问题。

您可以采取以下步骤来排除故障或可能解决此问题:

  • 确保您已在 Firebase 项目设置中添加正确的 OAuth 客户端 ID。这应该与您的应用程序的捆绑包 ID 匹配。

  • 在 app.json 或 app.config.js 中,确保设置了正确的包标识符

它可能看起来像这样:

{
    "expo": {
        "ios": {
            "bundleIdentifier": "your.app.bundleId"
       },
       "android": {
            "package": "your.app.packageName"
       }
    }
}
  • 您可以尝试将自定义 URL 方案添加到您的 Expo 配置中。这可能有助于受众匹配。

它会是这样的:

{
    "expo": {
        "scheme": "your-custom-scheme"
    }
}
  • 确保 Apple 开发者帐户和 Firebase 中的 Apple 登录配置匹配。 服务 ID 的格式应为

    your.app.bundleId.appleSignIn

  • 如果您还没有使用 expo-apple-authentication 包来处理 Apple 登录。

你可以这样做:

import * as AppleAuthentication from 'expo-apple-authentication';
   import { getAuth, signInWithCredential, OAuthProvider } from 'firebase/auth';

   async function signInWithApple() {
     try {
       const credential = await AppleAuthentication.signInAsync({
         requestedScopes: [
           AppleAuthentication.AppleAuthenticationScope.FULL_NAME,
           AppleAuthentication.AppleAuthenticationScope.EMAIL,
         ],
       });
       
       // Create an OAuthCredential using the Apple ID token
       const provider = new OAuthProvider('apple.com');
       const oauthCredential = provider.credential({
         idToken: credential.identityToken,
       });

       // Sign in to Firebase with the OAuth credential
       const auth = getAuth();
       const userCredential = await signInWithCredential(auth, oauthCredential);
       
       // User is signed in
       console.log('User signed in:', userCredential.user);
     } catch (e) {
       console.error('Error during Apple Sign-In:', e);
     }
}
  • 如果您在 Expo Go 客户端中进行测试,请尝试创建应用程序的开发版本。这将使用您的实际捆绑 ID,而不是 Expo 客户端的 ID。

    eas build --profile development --platform ios

  • 最后,您可以记录从 Apple 收到的 ID 令牌并对其进行解码(例如,在 jwt.io)以查看它包含哪些受众。这可以帮助查明不匹配发生的位置。


我希望至少能有其中一部作品。

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.