使用 phone 编号 returns 的 Firebase 身份验证出现内部错误

Firebase Auth using phone number returns an internal error

我将我的应用程序设置为能够使用 firebase 发送 Apple 通知,并且我使用控制台验证了它是否有效。现在我想做 phone 建立在 APN 之上的身份验证。

所以我写了这个:

PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber) { verificationID, error in
  if error != nil {
    print("Verification code not sent \(error!)")
  } else {
    print ("Successful.")
  }

我得到:

Error Domain=FIRAuthErrorDomain Code=17999 "An internal error has occurred, print and inspect the error details for more information." UserInfo={NSUnderlyingError=0x170046db0 {Error Domain=FIRAuthInternalErrorDomain Code=3 "(null)" UserInfo={FIRAuthErrorUserInfoDeserializedResponseKey={
    code = 500;
    message = "<null>";
}}}, error_name=ERROR_INTERNAL_ERROR, NSLocalizedDescription=An internal error has occurred, print and inspect the error details for more information.}

有什么想法吗?我应该针对 firebase 提交错误吗?

我正在使用 iOS SDK 4.0.0(我能找到的最新 zip。)

更新:

我通过将 FirebaseAppDelegateProxyEnabled 添加到 info.plist 并将其设置为 NO

来禁用方法调配
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Pass device token to auth.
    Auth.auth().setAPNSToken(deviceToken, type: .prod)
}

使用最新的 Firebase iOS SDK 进行测试,即 4.0.0Xcode 8.3

首先,从 info.plist 中删除此键 FirebaseAppDelegateProxyEnabled。这不是必需的。

现在在AppDelegate.swift中添加以下函数

import Firebase
import UserNotifications

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate , UNUserNotificationCenterDelegate{
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()
        FirebaseApp.configure()
        return true
    }
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Pass device token to auth.
    let firebaseAuth = Auth.auth()

    //At development time we use .sandbox
    firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)

    //At time of production it will be set to .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
    }
}*

发送验证码到用户phone:

在要集成Phone身份验证的class中写入:

注意 :我添加了 +91 作为印度的国家代码。您可以根据您的地区添加国家代码。

 PhoneAuthProvider.provider().verifyPhoneNumber("+919876543210") { (verificationID, error) in
       if ((error) != nil) {
             // Verification code not sent.
             print(error)
       } else {
              // Successful. User gets verification code 
              // Save verificationID in UserDefaults
             UserDefaults.standard.set(verificationID, forKey: "firebase_verification")
             UserDefaults.standard.synchronize()
             //And show the Screen to enter the Code.
       }               

用户使用验证码登录:

 let verificationID = UserDefaults.standard.value(forKey: "firebase_verification")
 let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationID! as! String, verificationCode: self.txtEmailID.text!)

   Auth.auth().signIn(with: credential, completion: {(_ user: User, _ error: Error?) -> Void in
         if error != nil {
            // Error
          }else {
             print("Phone number: \(user.phoneNumber)")
              var userInfo: Any? = user.providerData[0]
                    print(userInfo)
                }
         } as! AuthResultCallback)

仔细检查 Xcode 中的应用包 ID 是否与 Firebase 中的包 ID 完全匹配。并通过完全,确保他们的大小写匹配 - Xcode 喜欢默认使用混合大小写作为 bundle ID 的应用程序名称部分。

如果您最终在 Xcode 中更改了捆绑包 ID,请确保在 Xcode 中生成新配置文件之前手动删除该应用程序的配置文件,否则它将反复失败(Apple显然忽略了个人资料名称的大小写)。

在我的例子中,错误的是 apns 令牌类型:

Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod)

应该是:

Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)

嗯,在我的例子中,我发送了错误的 self.verificationIDFIRAuthCredential。 如果您遇到此错误,请打印您的 verificationID 并检查是否与您发送到 FIRAuthCredential.

的是同一个

这是我在 objC 中的代码:

[[FIRPhoneAuthProvider provider] verifyPhoneNumber:self.phoneNumberTextField.text
                                            UIDelegate:nil
                                            completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {
                                                if (error) {
                                                    NSLog(@"error %@", error.localizedDescription);
                                                    return;
                                                }
                                                NSLog(@"verificationID %@", verificationID);
                                                self.verificationID = [NSString stringWithFormat:@"%@", verificationID];

//                                                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//                                                [defaults setObject:verificationID forKey:@"authVerificationID"];
//                                                NSString *verificationID = [defaults stringForKey:@"authVerificationID"];

                                                // Sign in using the verificationID and the code sent to the user
                                                // ...
                                            }];

我在这里不小心发送了错误的 verificationID :

self.verificationID = [NSString stringWithFormat:@"verificationID",];

右边是这个:

self.verificationID = [NSString stringWithFormat:@"%@", verificationID];

然后我像这样将它发送到 FIRAuthCredential :

FIRAuthCredential *credential = [[FIRPhoneAuthProvider provider]
                                     credentialWithVerificationID:self.verificationID
                                     verificationCode:self.pinCodeTextField.text];

    [[FIRAuth auth] signInWithCredential:credential
                              completion:^(FIRUser *user, NSError *error) {
                                  if (error) {
                                      NSLog(@"error %@", error);
                                      return;
                                  }
                                  NSLog(@"Success");
                                  // User successfully signed in. Get user data from the FIRUser object
                                  // ...
                              }];

其中returnsuccess成功。希望对其他人有帮助。

我很容易解决 make type .sandbox

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Pass device token to auth.
    let firebaseAuth = Auth.auth()
    //At development time we use .sandbox
    firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)
}

并从代码中删除这一行

Auth.auth().settings.isAppVerificationDisabledForTesting = TRUE