Bittrex API returns APIKEY_NOT_PROVIDED
I am trying to call the Bittrex api in my sample iOS app.
I got the following message.
Return message
SUCCESS: {
message = "APIKEY_NOT_PROVIDED";
result = "<null>";
success = 0;
}
And I checked the HTTP request, apikey was included below.
API request
$ 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"
And my code is like the following.
code
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)
}
})
Environment
- macOS Sierra
- XCode 8.3.3
- Swift 3
- Alamofire 4.4
I believe you have a problem with the new V1.1 API where you need to encrypt the secret using HMAC-SHA512:
For this version, we are using the standard HMAC-SHA512 signature. Add
apikey
bothnonce
to your request and compute the HMAC hash and include it under the headerapisign
. Note: the nonce is not enforced right now but will be applied later.
Not an iOS expert, but this might help you:
$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);
looks like a simple typo / you should use api secret and a string that assumes what i believe should be a uri (not url). There is a fast BittrexKit library using the Crypro library - with a slightly different method signature.
let secretKey: String = URI.hmac(algorithm: CryptoAlgorithm.SHA512, key: APISECRET)
cherry picked from 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
}
(if anyone gets this working with alamofire - any changes are welcome.)