微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我正在使用 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 enabled
  2. 我为域添加my-example-app.firebaseapp.com,并在 Return URLs
  3. 添加https://my-example-app.firebaseapp.com/__/auth/handler

我的 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!
}

解决方法

这是 100% 由于服务器或客户端上的服务 ID 错误。

我正在开发一个项目,其中我们使用 Django 作为后端服务器,后端开发人员在服务器上使用了不同的服务 ID,而我在客户端使用了不同的服务 ID。

我们是如何解决这个问题的。

在常规设置中打开 Firebase 控制台,检查 ios 应用程序的包 ID,将其与 Xcode 中的包 ID 进行比较。确保服务 ID 正确,并且您拥有最新的配置文件,其中添加了服务 ID。

阅读本文以了解如何创建服务 ID。 https://firebase.google.com/docs/auth/android/apple?authuser=4

您需要在 firebase 控制台中添加您在上面创建的相同服务 ID,您需要在身份验证部分启用 apple auth 服务

在您的情况下,您需要添加服务 ID

co.myexampleapp.signinwithapple

在屏幕截图中显示的输入框中。

enter image description here

,

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

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

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

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

,

由于是token的问题,建议您检查以下内容。

  1. 确保您在 support email 项目设置中的 firebase 中提供了您的电子邮件。
  2. 在执行登录操作之前尝试注销。由于在开发过程中不正确的注销,这可能会发生。 确保始终在 logout 之前signin。在某些情况下帮助了我。
  3. 设备时间 - 因为生成的令牌将基于时间戳。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。