gpt4 book ai didi

ios - 我无法删除使用苹果登录的用户

转载 作者:行者123 更新时间:2023-12-05 05:40:28 27 4
gpt4 key购买 nike

我们正在实现 Apple 对用户删除施加的新 guidelines。我正在使用 firebase 进行身份验证。我正在创建一个用户帐户。这里没问题。但我无法删除使用苹果登录的用户。我收到错误。我正在使用此 api 删除 token revoke API 。我是 swift 的新手。我找不到问题

这是我的登录码。它工作正常:

class SignUpNewUser: NSObject {
/// apple
var currentNonce: String? // fileprivate
}

extension SignUpNewUser : ASAuthorizationControllerPresentationContextProviding{

func Apple() {
let nonce = randomNonceString()
currentNonce = nonce
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
request.nonce = sha256(nonce)

let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}

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()

return hashString
}

private func randomNonceString(length: Int = 32) -> String {
precondition(length > 0)
let charset: Array<Character> =
Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
var result = ""
var remainingLength = length

while remainingLength > 0 {
let randoms: [UInt8] = (0 ..< 16).map { _ in
var random: UInt8 = 0
let errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random)
if errorCode != errSecSuccess {
fatalError("Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)")
}
return random
}
randoms.forEach { random in
if length == 0 {
return
}

if random < charset.count {
result.append(charset[Int(random)])
remainingLength -= 1
}
}
}
return result
}
}



extension SignUpNewUser: ASAuthorizationControllerDelegate {


func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {

guard let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential else {
dataSource?.endSignUpWithError(error: eLogin.authorizationNotFound.error(), method: .apple)
return
}

guard let nonce = currentNonce else {
fatalError("Invalid state: A login callback was received, but no login request was sent.")
}

// JWT
guard let appleIDToken = appleIDCredential.identityToken else {
dataSource?.endSignUpWithError(error: eLogin.identityToken.error(), method: .apple)
return
}

guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
mylog(eLogin.serializeToken.error(), appleIDToken.debugDescription)
dataSource?.endSignUpWithError(error: eLogin.serializeToken.error(), method: .apple)
return
}

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

@AppStorage("JWt_client_secret") var client_secret: String = ""
@AppStorage("apltoken") var apple_token: String = ""
client_secret = nonce
apple_token = idTokenString

signInFirebaseAccount(credential, appleIDCredential:appleIDCredential)
}



/// signup tamam firebase bağla hesabı
private func signInFirebaseAccount(_ credential: AuthCredential, appleIDCredential: ASAuthorizationAppleIDCredential) {
Auth.auth().signIn(with: credential) { (authResult, error) in

if let error = error {
self.dataSource?.endSignUpWithError(error: error , method: .apple)
}else{
guard let user = authResult?.user else {
self.dataSource?.endSignUpWithError(error: eLogin.kayitHatasi.error(), method: .apple)
return
}

// JWT
guard let appleIDToken = appleIDCredential.identityToken else {
print("hata 5")
return
}

guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
print("hata 6")
return
}
guard let nonce = self.currentNonce else {
fatalError("Invalid state: A login callback was received, but no login request was sent.")
}

@AppStorage("JWt_client_secret") var client_secret: String = ""
@AppStorage("apltoken") var apple_token: String = ""
client_secret = nonce
apple_token = idTokenString


self.createNewUser(user: user, method: .apple)

}
}
}

}

我正在使用此代码删除帐户。这是行不通的。我正在添加运行代码后获得的控制台图像。

extension SignUpNewUser{
func deleteAppleAccount() {
@AppStorage("JWt_client_secret") var client_secret: String = ""
@AppStorage("apltoken") var apple_token: String = ""


let urlString = "https://appleid.apple.com/auth/revoke"
let url = NSURL(string: urlString)!

let paramString: [String : Any] = [
"client_id": "com.MyCompany.Name",
"client_secret": client_secret,
"token": apple_token
]


let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject:paramString, options: [.prettyPrinted])
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
// request.setValue("application/json", forHTTPHeaderField: "Accept")


let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
guard
let response = response as? HTTPURLResponse,
error == nil
else { // check for fundamental networking error
print("error", error ?? URLError(.badServerResponse))
return
}

guard (200 ... 299) ~= response.statusCode else { // check for http errors
print("statusCode should be 2xx, but is \(response.statusCode)")
print("response = \(response)")
return
}


if let error = error {
print(error)
}else{
print("deleted accont")
}
}
task.resume()
}

}

控制台输出:

statusCode should be 2xx, but is 400
response = <NSHTTPURLResponse: 0x2815e5e00> { URL: https://appleid.apple.com/auth/revoke } { Status Code: 400, Headers {
"Cache-Control" = (
"no-store"
);
Connection = (
"keep-alive"
);
"Content-Length" = (
26
);
"Content-Type" = (
"application/json;charset=ISO-8859-1"
);
Date = (
"Sat, 28 May 2022 12:07:51 GMT"
);
Pragma = (
"no-cache"
);
Server = (
Apple
);
} }

我哪里错了?我找不到任何关于它的教程。帐户删除选项将在当月 30 日之后强制执行。我现在需要解决这个问题:(

最佳答案

如果您不刷新 token 用户,您应该试试这个...

let paramString: [String : Any] = [
"client_id": "com.MyCompany.Name",
"client_secret": client_secret,
"token": apple_token,
"token_type_hint": "access_token"
]

如果你这样做

let paramString: [String : Any] = [
"client_id": "com.MyCompany.Name",
"client_secret": client_secret,
"token": apple_token,
"token_type_hint": apple_typeToken

]

其中 apple_typeToken 可以是“access_token”或“refresh_token”

你应该像这样编辑你的请求。httpMethod

HTTP Request in Swift with POST method

关于ios - 我无法删除使用苹果登录的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72415745/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com