json - 如何在 Swift 中进行签名 API 调用

标签 json swift api decodable binance

我正在尝试使用 HMAC SHA256 签名执行签名的 API 请求。

我如何提出签名请求?我无法生成正确的签名:

{"code":-1022,"msg":"Signature for this request is not valid."}

  static func binanceAccountSnapshot(timeStamp: Int) {
        
        let semaphore = DispatchSemaphore (value: 0)
        let urlWithoutSignature = "https://api.binance.com/sapi/v1/capital/config/getall?timestamp=\(timeStamp)"
        let secretString = "aN345refdcx78iygkhbrefdoyilhukB6prefd98uoixjk(api secret)"
        let key = SymmetricKey(data: secretString.data(using: .utf8)!)
        
        let signature = HMAC<SHA256>.authenticationCode(for: urlWithoutSignature.data(using: .utf8)!, using: key)
        let signatureString = Data(signature).map { String(format: "%02hhx", $0) }.joined()
         
        var request = URLRequest(url: URL(string: "https://api.binance.com/sapi/v1/capital/config/getall?timestamp=\(timeStamp)&signature=\(signatureString)")!,timeoutInterval: Double.infinity)
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("p4t98weflsudichjkxwtrfsduoxhckjnwe8isdokjx(api key)", forHTTPHeaderField: "X-MBX-APIKEY")
        request.httpMethod = "GET"
         
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data else {
                print(String(describing: error))
                semaphore.signal()
                return
            }
            print(String(data: data, encoding: .utf8)!)
            semaphore.signal()
        }
        task.resume()
    }

最佳答案

你的代码几乎可以工作,签名的字符串应该只是网址的参数,而不是整个网址(字符串应该只是网址中 ? 之后的那些字符)

  static func binanceAccountSnapshot(timeStamp: Int) {
        
        let semaphore = DispatchSemaphore (value: 0)
        let params = timestamp=\(timeStamp)
        let urlWithoutSignature = "https://api.binance.com/sapi/v1/capital/config/getall?\(params)"
        let secretString = "aN345refdcx78iygkhbrefdoyilhukB6prefd98uoixjk(api secret)"
        let key = SymmetricKey(data: secretString.data(using: .utf8)!)
        
        let signature = HMAC<SHA256>.authenticationCode(for: params.data(using: .utf8)!, using: key)
        let signatureString = Data(signature).map { String(format: "%02hhx", $0) }.joined()
         
        var request = URLRequest(url: URL(string: "https://api.binance.com/sapi/v1/capital/config/getall?timestamp=\(timeStamp)&signature=\(signatureString)")!,timeoutInterval: Double.infinity)
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("p4t98weflsudichjkxwtrfsduoxhckjnwe8isdokjx(api key)", forHTTPHeaderField: "X-MBX-APIKEY")
        request.httpMethod = "GET"
         
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data else {
                print(String(describing: error))
                semaphore.signal()
                return
            }
            print(String(data: data, encoding: .utf8)!)
            semaphore.signal()
        }
        task.resume()
    }

关于json - 如何在 Swift 中进行签名 API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66921923/

相关文章:

c# - 当数据过大时,发布请求不起作用 .net 核心 web api

c# - 如何使用 DataContractJsonSerializer 序列化匿名类型

ruby-on-rails - Rails:Net::OpenTimeout 执行因 HTTParty 过期

iOS:在 Swift3 中,在我的 collectionView 中,当我向下滚动时,collectionView 自动出现在顶部。如何阻止它?

php - 如何在wordpress中使用json API获取上传的文件

node.js - 是否有 Raml 1.0(不是 0.8)HTML 生成器?

ajax - @RequestBody 不工作 - 在 Jquery 中返回 Http 状态 415 不受支持的媒体类型

xcode - 无限旋转按钮 : Swift

swift - 一个 Realm 对象如何在不被删除的情况下变得无效

regex - Jax-RS 重载方法/路径执行顺序