我一直关注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.