如何解决Firebase REST API signInWithIdp 和 Apple SignIn Provider - MISSING_OR_INVALID_NONCE
我正在 React Native 应用程序中实现 Apple 登录。
我使用的示例在移动应用中运行良好: Apple Sign In - React Native Firebase
以防万一上面的链接发生变化,这里是代码:
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。
我/我所做的是使用此处获得的 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 是支持“使用 OAuth 凭据登录”的文档。它给出的示例如下所示:
id_token=[GOOGLE_ID_TOKEN]&providerId=[google.com]
您实际上也可以添加一个 nonce
参数(未记录在案!),例如:
id_token=ID_TOKEN&providerId=apple.com&nonce=NONCE
为了解决这个问题,我必须使用 SDK(我使用 iOS)并发出请求。我用mitmproxy来读取网络,这是它在放入虚拟数据时显示的内容:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。