尝试使用firebase验证电话号码时出现"无效令牌"

Jor*_*ate 15 ios firebase swift firebase-authentication swift3

这是我的代码:

import FirebaseAuth


class AuthPhoneNum {

    static func getPhoneNum(phoneNumber: String) {
        PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber) { (verificationID, error) in
            if let error = error {
                print(error)
                return
            }
            UserDefaults.standard.set(verificationID, forKey: "authVerificationID")
        }
    }

    static func verify(verificationCode: String?) {
        guard let verificationID = UserDefaults.standard.string(forKey: "authVerificationID") else { return }
        if verificationCode != nil {
            let credential = PhoneAuthProvider.provider().credential(
                withVerificationID: verificationID,
                verificationCode: verificationCode!)

            Auth.auth().signIn(with: credential) { (user, error) in
                if let error = error {
                    print(error)
                    return
                }
            }
        } else {
            print("No verification code")
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这是控制台打印出来的:

错误域= FIRAuthErrorDomain代码= 17048"无效令牌." UserInfo = {NSLocalizedDescription =无效令牌.,error_name = INVALID_APP_CREDENTIAL}

我究竟做错了什么?谢谢

alg*_*rid 11

我也遇到了这个问题.检查以下内容:

  • 正确的捆绑ID
  • 更正Google-Info.plist
  • 正确的aps-environment价值
  • 调用时更正APNS令牌类型auth.setAPNStoken(.unknown用于自动检测)

没有任何帮助,直到在Firebase应用程序设置中我上传了APNS身份验证密钥(p8)而不是证书 - 我之前只使用这些证书进行推送通知,一切正常但是电话号码通知出错了.

  • @algrid `aps-environment` 在哪里? (3认同)
  • 你在哪里上传p12? (2认同)

小智 11

您很可能需要
在 Apple Developer Account 中上传一个 .p8 密钥文件(我有一个企业帐户,但对于开发人员来说也是如此):

  • 所有键
  • 创建新密钥 (+) 在此处输入图片说明
  • 输入所有应用程序的全局名称
  • Apple 推送通知服务 (APN) 旁边的复选框 在此处输入图片说明
  • 下载您的 p8 文件
  • 上传到 Firebase 仪表板 在此处输入图片说明


Aru*_*r P 7

首先重新生成 APNS 密钥并上传到 Firebase 以进行云消息传递

1) 导入 Firebase 和 FirebaseAuth

import Firebase
import FirebaseAuth
Run Code Online (Sandbox Code Playgroud)

2) 在 didFinishLaunchingWithOptions 中配置 firebase。

FirebaseApp.configure()
Run Code Online (Sandbox Code Playgroud)

3) 在 AppDelegate 中编写这两个函数。

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let firebaseAuth = Auth.auth()
    firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod)

}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    let firebaseAuth = Auth.auth()
    if (firebaseAuth.canHandleNotification(userInfo)){
        print(userInfo)
        return
    }
}
Run Code Online (Sandbox Code Playgroud)

非常重要的提示:uthAPNSTokenType正确设置 [沙箱/生产] 或设置为普通.unknown

就我而言,这是错误的 apns 令牌类型:

Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod)
Run Code Online (Sandbox Code Playgroud)

本来应该:

Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)
Run Code Online (Sandbox Code Playgroud)


Par*_*rth 5

同样的问题之前也曾出现过。所以想告诉您在运行代码之前设置所有预先要求的步骤。

预要求步骤:

  • 在开发者帐户上注册捆绑包 ID 并启用捆绑包 ID 的通知。

  • 在firebase控制台设置页面上注册相同的bundle id并创建应用程序,下载Google-Info.plist文件,确保名称应该相同。

  • 在 firebase 控制台上上传推送证书以进行沙箱和开发。

  • 请点击以下链接进行代码实现。

Firebase Auth 的设置代码


Ign*_*eso 5

就我而言,问题是iOS 应用程序中 Firebase 项目设置(项目设置 -> 常规 -> 您的应用程序)中的捆绑包 ID 不正确。

我希望这可以帮助任何忽略相同细节的人。