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



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

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

{"代码":-1022,"消息":"此请求的签名无效。"}

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

您的代码几乎可以工作,签名的字符串应该只是url的params,而不是整个url(字符串应该只是url中?之后的字符(

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

相关内容

  • 没有找到相关文章

最新更新