Firebase REST API signInWithIdp 和 Apple 登录提供商 - MISSING_OR_INVALID_NONCE

Firebase REST API signInWithIdp and Apple SignIn Provider - MISSING_OR_INVALID_NONCE

我正在 React Native 应用程序中实现 Apple 登录。

我使用的示例在移动应用程序中运行良好: Apple Sign In - React Native Firebase

为了防止上面的 link 发生变化,这里是代码:

import auth from '@react-native-firebase/auth';
import { appleAuth } from '@invertase/react-native-apple-authentication';

async function onAppleButtonPress() {
  // Start the sign-in request
  const appleAuthRequestResponse = await appleAuth.performRequest({
    requestedOperation: appleAuth.Operation.LOGIN,
    requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME],
  });

  // Ensure Apple returned a user identityToken
  if (!appleAuthRequestResponse.identityToken) {
    throw 'Apple Sign-In failed - no identify token returned';
  }

  // Create a Firebase credential from the response
  const { identityToken, nonce } = appleAuthRequestResponse;
  const appleCredential = auth.AppleAuthProvider.credential(identityToken, nonce);

  // Sign the user in with the credential
  return auth().signInWithCredential(appleCredential);
}

在我的后端应用程序只有在使用 Firebase 访问令牌时才有权进行调用。

为了让它工作,我正在使用 Google/Firebase REST API signInWithIdp。这对 Twitter/Facebook/Google 非常有效,但不适用于 Apple。

I/m所做的是使用此处获取的Apple identityToken:

const { identityToken, nonce } = appleAuthRequestResponse;

然后我打了这个电话:

POST https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key={key}

BODY:
{
"postBody":"id_token={identityToken}&providerId=apple.com",
"requestUri":"http://localhost",
"returnSecureToken": true
}

但我得到:

{
    "error": {
        "code": 400,
        "message": "MISSING_OR_INVALID_NONCE : Duplicate credential received. Please try again with a new credential.",
        "errors": [
            {
                "message": "MISSING_OR_INVALID_NONCE : Duplicate credential received. Please try again with a new credential.",
                "domain": "global",
                "reason": "invalid"
            }
        ]
    }
}

由于缺少文档,我无法使用此文档。 :(

非常欢迎任何有关如何消除此错误的建议。 :)

Here 是支持'Sign in with OAuth credential' 的文档。它给出的示例如下所示:

id_token=[GOOGLE_ID_TOKEN]&providerId=[google.com]

您实际上也可以添加一个 nonce 参数(未记录!),例如:

id_token=ID_TOKEN&providerId=apple.com&nonce=NONCE

为了解决这个问题,我必须使用 SDK(我使用 iOS)并发出请求。我使用 mitmproxy 读取网络,这是它在输入虚拟数据时显示的内容: