我试图在我的示例iOS应用程序中调用Bittrex api。
我收到以下消息。
返回消息
SUCCESS: {
message = "APIKEY_NOT_PROVIDED";
result = "<null>";
success = 0;
}
我检查了HTTP请求,apikey包含在下面。
API请求
$ curl -i \
-b "__cfduid=dea9a55b977dded0f94ad14f7158f03e91497078592" \
-H "Content-Type: application/json" \
-H "Accept-Language: en;q=1.0" \
-H "apisign: 41a5b3a98d7da9bee01ddb8ab72dee...." \
-H "User-Agent: SampleApp/1.0 (com.sampleapp.SampleApp; build:1; iOS 10.3.1) Alamofire/4.4.0" \
-H "Accept-Encoding: gzip;q=1.0, compress;q=0.5" \
-d "{\"apikey\":\"f23439b...\",\"nonce\":\"1497703898.0\"}" \
"https://bittrex.com/api/v1.1/account/getbalances"
我的代码就像是。
代码
let timeInterval = NSDate().timeIntervalSince1970
let epochtime = String(floor(timeInterval))
let parameters = "apikey=" + "f23439b..." + "&" + "nonce=" + epochtime
let url = path + "getbalances" + "?" + parameters
let secretKey: String = "df8a62...".hmac(algorithm: CryptoAlgorithm.SHA512, key: url)
Alamofire.request(path + "getbalances",
method: .get,
parameters: ["apikey":"f23439b...", "nonce":epochtime],
encoding: JSONEncoding.default,
headers: ["apisign":secretKey])
.responseJSON(completionHandler: { response in
let json = response.result.value as! NSDictionary
let success = json["success"] as! CFBoolean
if (success == kCFBooleanTrue) {
//do something
print(json)
} else {
let reason = response.result.description as AnyObject
print(reason)
}
})
环境
答案 0 :(得分:6)
我相信您的新V1.1 API存在问题,需要使用HMAC-SHA512加密Secret:
对于此版本,我们使用标准HMAC-SHA512签名。将
apikey
和nonce
附加到您的请求并计算HMAC哈希并将其包含在apisign
标头下。注意:nonce目前不受尊重,但稍后会强制执行。
不是iOS专家,但这可能对您有帮助:
$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);
答案 1 :(得分:0)
看起来像一个简单的错字/你应该使用api秘密和字符串预设我相信应该是uri(不是网址)。有一个快速的BittrexKit库使用Crypro库 - 方法签名略有不同。
let secretKey: String = URI.hmac(algorithm: CryptoAlgorithm.SHA512, key: APISECRET)
樱花摘自
https://github.com/yuzushioh/BittrexKit
import Crypto
guard let sign = HMAC.sign(message: uri, algorithm: .sha512, key: auth.apiSecret) else {
print("Bittrex: HMAC failed")
}
private func buildURI() -> String? {
let url = path.isEmpty ? baseURL : baseURL.appendingPathComponent(path)
guard var components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
return nil
}
if let queryParameters = queryParameters, !queryParameters.isEmpty {
components.percentEncodedQuery = URLEncodedSerialization.string(from: queryParameters)
}
guard let uri = components.url?.absoluteString else {
return nil
}
return uri
}
(如果有人使用alamofire进行此操作 - 我欢迎任何修改。)