You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Expo React Native中Apple Sign In关联Firebase用户时出现auth/argument-error错误

Expo React Native中Apple Sign In关联Firebase用户时出现auth/argument-error错误

我之前也碰到过类似的问题,结合你的代码和配置来看,大概率是几个关键细节没处理对,咱们一步步来排查解决:

1. 先修复app.json的插件配置错误

你现在把expo-apple-authentication@stripe/stripe-react-native的插件配置嵌套在一起了,这不符合Expo插件的配置规范,会导致苹果认证的插件无法正确加载。正确的配置应该是把两个插件分成独立的数组项:

{
  ...
  "plugins": [
    "expo-apple-authentication",
    [
      "@stripe/stripe-react-native",
      {
        "merchantIdentifier": "XXXXXXXXXXXXXXXXXXXX",
        "enableGooglePay": false
      }
    ]
  ]
}

修改完之后记得重新运行expo prebuild或者expo start --clear,确保配置生效。

2. 确认Firebase里的Apple Sign In Service ID配置正确

你怀疑Service ID的问题是对的,Firebase里填写的不是{你的Team ID}.{Bundle ID},而是需要在Apple开发者后台专门创建的Sign In with Apple Service ID:

  • 登录苹果开发者后台,进入「Certificates, Identifiers & Profiles」
  • 点击「Identifiers」→ 右上角「+」→ 选择「Services IDs」创建新的Service ID
  • 填写ID(比如com.yourdomain.apple-signin)和描述,开启「Sign In with Apple」功能
  • 配置这个Service ID的「Primary App ID」为你的项目Bundle ID,然后添加Firebase提供的回调地址(在Firebase控制台的Apple认证设置里可以找到,格式一般是https://<你的项目ID>.firebaseapp.com/__/auth/handler
  • 回到Firebase控制台的Apple认证设置,把刚才创建的Service ID填进去,同时确保你已经上传了有效的Apple Sign In Key(.p8文件),并且填对了Key ID和Team ID

3. 调整代码里的Firebase凭证构建逻辑

auth/argument-error很多时候是因为凭证参数不完整或者格式不对,你可以试试加上rawNonce的处理(Firebase要求nonce和identityToken对应,防止重放攻击):

首先需要安装expo-crypto(如果还没装的话),然后修改代码:

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

// 生成符合要求的nonce
const generateNonce = async () => {
  const randomBytes = await Crypto.getRandomBytesAsync(32);
  return Crypto.bytesToBase64(randomBytes)
    .replace(/\+/g, '-')
    .replace(/\//g, '_')
    .replace(/=+$/, '');
};

const createUserWithApple = async () => {
  try {
    const rawNonce = await generateNonce();
    const credential = await AppleAuthentication.signInAsync({
      requestedScopes: [
        AppleAuthentication.AppleAuthenticationScope.FULL_NAME,
        AppleAuthentication.AppleAuthenticationScope.EMAIL,
      ],
      nonce: rawNonce, // 把生成的nonce传给苹果认证
    });

    const { identityToken } = credential;
    if (identityToken) {
      console.log("ID TOKEN:", identityToken);
      const provider = new OAuthProvider("apple.com");
      // 构建凭证时同时传入identityToken和rawNonce
      const providerCredential = provider.credential({ 
        identityToken,
        rawNonce,
      });
      const result = await signInWithCredential(FIREBASE_AUTH, providerCredential);
      console.log("GOT RESULT:", result);
    }
  } catch (error) {
    console.log("CAUGHT ERROR:", error);
  }
};

另外还要确认:

  • 你的Firebase Auth实例FIREBASE_AUTH已经正确初始化(比如通过getAuth(app)获取)
  • 你的Firebase SDK版本是最新的,旧版本可能对Apple Sign In的支持有兼容性问题

4. 最后验证苹果开发者后台的基础配置

  • 确认你的App ID已经开启了「Sign In with Apple」功能
  • 确认你创建的Apple Sign In Key是有效的,并且Key ID和Team ID都正确填写到了Firebase里

按照上面的步骤一步步排查,应该就能解决这个auth/argument-error问题了。

备注:内容来源于stack exchange,提问作者David Henry

火山引擎 最新活动