英文:
Error status code 400 when trying to delete a RevenueCat customer with REST, what could be the problem?
问题
以下是您的代码的中文翻译:
我正在尝试从RevenueCat中删除一个客户,如果用户决定删除他们的帐户。这是我实现的代码:
private func deleteRevenueCatAccount(forUserID: String) {
    
    let headers = ["accept": "application/json", "Authorization": "Bearer \(secretApiKey)"]
    let baseURL = "https://api.revenuecat.com/v1"
    let deleteEndpoint = "/subscribers/\(forUserID)"
    let urlString = baseURL + deleteEndpoint
    
    guard let url = URL(string: urlString) else {
        // 处理无效的URL错误
        return
    }
    
    // 创建请求
    let request = NSMutableURLRequest(
        url: url,
        cachePolicy: .useProtocolCachePolicy,
        timeoutInterval: 10.0
    )
    
    request.httpMethod = "DELETE"
    request.allHTTPHeaderFields = headers
    
    // 创建会话
    let session = URLSession.shared
    
    // 发送请求
    let task = session.dataTask(with: request as URLRequest) { (data, response, error) in
        
        if let error = error {
            // 处理错误
            print("错误:\(error.localizedDescription)")
            return
        }
        
        // 检查响应
        if let httpResponse = response as? HTTPURLResponse {
            
            if httpResponse.statusCode == 204 {
                // 客户成功删除
                print("客户成功删除")
            } else {
                // 处理非成功状态码
                print("错误:\(httpResponse.statusCode)")
            }
            
        } //: HTTP响应
        
    } //: 数据任务
    
    task.resume()
    
} //: FUNC
然而,它在HTTPURLResponse中抛出一个带有状态码400的错误。有任何想法可能是问题吗?
注意:
我试图删除的用户是沙盒用户,因为我仍在开发中,但在联系了RevenueCat支持之后,他们告诉我它应该仍然适用于沙盒用户。
英文:
I am trying to delete a customer from RevenueCat if the user decides to delete their account. This is the code I implemented:
private func deleteRevenueCatAccount(forUserID: String) {
    
    let headers = ["accept": "application/json", "Authorization": "Bearer \(secretApiKey)"]
    let baseURL = "https://api.revenuecat.com/v1"
    let deleteEndpoint = "/subscribers/\(forUserID)"
    let urlString = baseURL + deleteEndpoint
    
    guard let url = URL(string: urlString) else {
        // Handle invalid URL error
        return
    }
    
    // Create the request
    let request = NSMutableURLRequest(
        url: url,
        cachePolicy: .useProtocolCachePolicy,
        timeoutInterval: 10.0
    )
    
    request.httpMethod = "DELETE"
    request.allHTTPHeaderFields = headers
    
    // Create the session
    let session = URLSession.shared
    
    // Send the request
    let task = session.dataTask(with: request as URLRequest) { (data, response, error) in
        
        if let error = error {
            // Handle error
            print("Error: \(error.localizedDescription)")
            return
        }
        
        // Check the response
        if let httpResponse = response as? HTTPURLResponse {
            
            if httpResponse.statusCode == 204 {
                // Customer deleted successfully
                print("Customer deleted successfully")
            } else {
                // Handle non-successful status code
                print("Error: \(httpResponse.statusCode)")
            }
            
        } //: HTTP RESPONSE
        
    } //: DATA TASK
    
    task.resume()
    
} //: FUNC
However, it throws an error with a status code 400 in the HTTPURLResponse.
Any ideas what could be the problem?
**Note:
The user that I am trying to delete is a sandbox user since I am still in development, but after contacting the RevenueCat support, they told me that it should still work for sandbox users.
答案1
得分: 1
我在RevenueCat工作,
如果我不看请求的响应正文,只是猜测的话,可能是您没有在请求标头中明确指定Content-Type: application/json,请尝试添加它:
 let headers = ["accept": "application/json", "Content-Type": "application/json", "Authorization": "Bearer \(secretApiKey)"]
然而,您不应该在应用程序内部使用秘密API密钥,这些密钥应该安全地用于您的服务器,否则恶意行为者可能会对您的帐户执行破坏性操作!
英文:
I work at RevenueCat,
If I had to guess, without looking at the response body of the request, it might be that you didn't explicitly state Content-Type: application/json in the request headers, try adding it:
 let headers = ["accept": "application/json", "Content-Type": "application/json", "Authorization": "Bearer \(secretApiKey)"]
However you shouldn't use a Secret API key inside your App, these are meant to be securely used in your server, otherwise bad actors could perform destructive actions on your account!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论