0

I'm attempting to use reqwest to connect to the kucoin api. I've adapted it to Kucoin's configuration from my Bybit implementation. I'm using the https://github.com/escwdev/kucoin_rs/blob/a79a0a08d1bf3dbca781a97bd9e15f645bef4a62/src/kucoin/client.rs format yet mine is unable to connect returning 401 relating to 'Unauthorized -- Invalid API Key'.

let url = "https://api.kucoin.com/api/v2/sub/user";
let req_url = reqwest::Url::parse(&url).unwrap();

let nonce = get_time().to_string();

let api_key = dotenv::var("KUCOIN_KEY").unwrap();

let secret_key = dotenv::var("KUCOIN_SECRET").unwrap();
let passphrase = dotenv::var("KUCOIN_PASSPHRASE").unwrap();

let str_to_sign = format!("{}{}{}", nonce, "GET", "/api/v2/sub/user");


let mut hmac_sign = <HmacSha256 as hmac::Mac>::new_varkey(secret_key.as_bytes())
    .expect("HMAC can take key of any size");
hmac_sign.input(str_to_sign.as_bytes());
let sign_result = hmac_sign.result();
let sign_bytes = sign_result.code();
let sign_digest = encode(&sign_bytes.as_ref());
let mut hmac_passphrase =
    HmacSha256::new_varkey(secret_key.as_bytes()).expect("HMAC can take key of any size");
hmac_passphrase.input(passphrase.as_bytes());
let passphrase_result = hmac_passphrase.result();
let passphrase_bytes = passphrase_result.code();
let passphrase_digest = encode(&passphrase_bytes.as_ref());
let mut headers = HeaderMap::new();

headers.insert(
    HeaderName::from_static("kc-api-key"),
    HeaderValue::from_str(&api_key).unwrap(),
);
headers.insert(
    HeaderName::from_static("kc-api-sign"),
    HeaderValue::from_str(&sign_digest).unwrap(),
);
headers.insert(
    HeaderName::from_static("kc-api-timestamp"),
    HeaderValue::from_str(&nonce).unwrap(),
);
headers.insert(
    HeaderName::from_static("kc-api-passphrase"),
    HeaderValue::from_str(&passphrase_digest).unwrap(),
);
headers.insert(
    HeaderName::from_static("kc-api-key-version"),
    HeaderValue::from_static("2"),
);

let client = reqwest::Client::new();
//
let request = client
    .get(req_url)
    .headers(headers)
    .send()
    .await
    .map_err(|e| Error::ReqwestError(e.to_string()));

Any pointers why my key is failing?

Jmb
  • 18,893
  • 2
  • 28
  • 55
Escork
  • 1
  • 1
  • 1
    Does the same api_key works when you use the lib (from which you adapted code) directly? A quick guess would be that something is wrong with your api_key - either C&P error or maybe not read from env properly. – draganstankovic Jul 31 '23 at 02:29
  • Yeah the previous implementation was using this library. But to make things generic I created my own – Escork Jul 31 '23 at 12:32

0 Answers0