反应原生 android firebase 登录与苹果 [错误:提供的身份验证凭据格式错误,已过期或当前不受支持。]

4 L*_*ver 5 android firebase react-native firebase-authentication invertase

我正在使用 Apple 实现登录。我可以成功看到Apple登录页面。我输入正确的凭据。它应该能够根据 Apple 的返回值登录/注册 Firebase。

但是我收到了这个错误Error: The supplied auth credential is malformed, has expired or is not currently supported。Firebase 方面一定有问题吗?onPressAppleLogin逻辑上可以参考下面的函数。非常感谢!

我做了什么:

在 Firebase 中

  1. 启用Apple 登录提供程序的身份验证
  2. 我的服务 ID 是co.myexampleapp.signinwithapple
  3. 我的授权回调是https://my-example-app.firebaseapp.com/__/auth/handler

在 developer.apple.com 中

  1. 我创建了一个服务 ID co.myexampleapp.signinwithapple启用了该服务Sign In with Apple
  2. 我加my-example-app.firebaseapp.com为域和https://my-example-app.firebaseapp.com/__/auth/handlerReturn URLs

我的 React Native 源代码

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

getRandomString = (length: any) => {
    let randomChars =
        'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    let result = ''
    for (let i = 0; i < length; i++) {
        result += randomChars.charAt(Math.floor(Math.random() * randomChars.length))
    }
    return result
}

onPressAppleLogin = async () => {
    const rawNonce = this.getRandomString(20);
    const state = this.getRandomString(20)

    appleAuthAndroid.configure({
        clientId: 'co.myexampleapp.signinwithapple',
        redirectUri: 'https://my-example-app.firebaseapp.com/__/auth/handler',
        responseType: appleAuthAndroid.ResponseType.ALL,
        scope: appleAuthAndroid.Scope.ALL,
        nonce: rawNonce,
        state,
    });

    const response = await appleAuthAndroid.signIn();

    const appleCredential = await firebase.auth.AppleAuthProvider.credential(response.id_token, rawNonce)

    const appleUserCredential = await firebase.auth().signInWithCredential(appleCredential) // error happens here!
}
Run Code Online (Sandbox Code Playgroud)

小智 1

初始化凭据时出现某种错误。

可能会出现三种类型的错误:

  1. 在响应中,令牌可能已过期。此时您可以使用刷新令牌功能来获取新令牌。
  2. 查看 firebase 中的规则,如果您在锁定模式下初始化应用程序,则读写将设置为 false。如果是这样,请将其更改为 true。
  3. 检查您是否启用了 Api 密钥。

重要检查您是否为 Apple ID 启用了第三方访问。