使用Apple为Firebase IOS13 Swift设置登录时出错的原因?

我一直关注https://firebase.google.com/docs/auth/ios/apple,但确实提到了我收到的错误,该错误是“将SHA256哈希值的随机数作为十六进制字符串发送”,但它没有任何帮助来解决它,我的搜索还没有成功。给我一个可行的解决方案。

我的视图控制器代码摘录是


        fileprivate var currentNonce: String?

        @objc @available(iOS 13,*)
        func startSignInWithAppleFlow() {
          let nonce = randomNonceString()
          currentNonce = nonce
          let appleIDProvider = ASAuthorizationAppleIDProvider()
          let request = appleIDProvider.createRequest()
          request.requestedScopes = [.fullName,.email]
          request.nonce = sha256(nonce)
            print(request.nonce)

          let authorizationController = ASAuthorizationController(authorizationRequests: [request])
          authorizationController.delegate = self
          authorizationController.presentationContextProvider = self
          authorizationController.performRequests()
        }
        @available(iOS 13,*)
        private func sha256(_ input: String) -> String {
          let inputData = Data(input.utf8)
          let hashedData = SHA256.hash(data: inputData)
          let hashString = hashedData.compactMap {
            return String(format: "%02x",$0)
          }.joined()
            print(hashString)
          return hashString
        }
    }
    @available(iOS 13.0,*)
    extension LoginViewController: ASAuthorizationControllerDelegate {

      func authorizationController(controller: ASAuthorizationController,didCompleteWithAuthorization authorization: ASAuthorization) {
        if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
          guard let nonce = currentNonce else {
            fatalError("Invalid state: A login callback was received,but no login request was sent.")
          }
          guard let appleIDToken = appleIDCredential.identityToken else {
            print("Unable to fetch identity token")
            return
          }
          guard let idTokenString = String(data: appleIDToken,encoding: .utf8) else {
            print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
            return
          }
          // Initialize a Firebase credential.
            print(nonce)
            let credential = OAuthProvider.credential(withProviderID: "apple.com",idToken: idTokenString,accessToken: nonce)

            print(credential)
          // Sign in with Firebase.
          Auth.auth().signInAndRetrieveData(with: credential) { (authResult,error) in
            if (error != nil) {
              // Error. If error.code == .MissingOrInvalidNonce,make sure
              // you're sending the SHA256-hashed nonce as a hex string with
              // your request to Apple.
                print(authResult)
                print(error!)
                print(error!.localizedDescription)
              return
            }
            // User is signed in to Firebase with Apple.
            // ...
          }
        }
      }

此部分与网页上的说明不同,因为Xcode给出了错误


    let credential = OAuthProvider.credential(withProviderID: "apple.com",accessToken: nonce)

如果我在紧接之前打印随机数


    let credential = OAuthProvider.credential(withProviderID: "apple.com",accessToken: nonce)

我得到2eNjrtagc024_pd3wfnt_PZ0N89GZ_b6_QJ3IZ _

response.nonce值为cd402f047012a2d5c129382c56ef121b53a679c0a5c5e37433bcde2967225afe

显然这些并不相同,但我似乎无法弄清自己做错了什么。

完整错误输出为

Error Domain=FIrauthErrorDomain Code=17999 "An internal error has occurred,print and inspect the error details for more information." UserInfo={NSUnderlyingError=0x60000388a820 {Error Domain=FIrauthInternalErrorDomain Code=3 "(null)" UserInfo={FIrauthErrorUserInfoDeserializedResponseKey={
    code = 400;
    errors =     (
                {
            domain = global;
            message = "MISSING_OR_INVALID_NONCE : Nonce is missing in the request.";
            reason = invalid;
        }
    );
    message = "MISSING_OR_INVALID_NONCE : Nonce is missing in the request.";
}}},FIrauthErrorUserInfoNameKey=ERROR_INTERNAL_ERROR,error_name=ERROR_INTERNAL_ERROR,NSLocalizedDescription=An internal error has occurred,print and inspect the error details for more information.}
An internal error has occurred,print and inspect the error details for more information.
yuyong89 回答:使用Apple为Firebase IOS13 Swift设置登录时出错的原因?

我遇到了同样的错误。

解决方案: 只需运行

pod update

说明:

问题就如@ethanrj所说。文档说要做

let credential = OAuthProvider.credential( 
    withProviderID: "apple.com",IDToken: appleIdToken,rawNonce: rawNonce )

但这会产生错误,Xcode会建议以下内容(rawNonce-> accessToken):

let credential = OAuthProvider.credential( 
    withProviderID: "apple.com",accessToken: rawNonce )

这是因为pod install在您真正需要6.13时会默认安装FirebaseAuth 6.12,因为新功能签名仅在此处可用。您可以在此处(https://github.com/firebase/firebase-ios-sdk/blob/master/Firebase/Auth/Source/AuthProvider/OAuth/FIROAuthProvider.m)查看源。

,

是否可能使用了错误的凭证方法?在documentation上,看起来像现时一样的是:

 let credential = OAuthProvider.credential( withProviderID: "apple.com",rawNonce: rawNonce )

但是您在这里使用的那个需要访问令牌,不确定是否有帮助。

本文链接:https://www.f2er.com/3044400.html

大家都在问