无法弄清楚如何向 OKEx 发送签名的 POST 请求

系统管理员

我想向 Okex 发送一个签名的 POST 请求:Authentication Docs POST Request Docs

我总是收到“无效符号”错误。

我成功发送了一个签名的 GET 请求。对于 POST,您还需要在签名中添加正文。如果我这样做,我的签名都不再有效。我已经确认我的签名与他们的官方 Python SDK 生成的签名相同(这就是我手工编写 JSON 的原因。Python 在 JSON 中有空格)。我是 Rust 的新手,所以我希望我遗漏了一些明显的东西。

其他语言的OKEx客户端实现:https : //github.com/okcoin-okex/open-api-v3-sdk

/// [dependencies]
/// hmac="0.7.1"
/// reqwest = "0.9.18"
/// chrono = "0.4.6"
/// base64="0.10.1"
/// sha2="0.8.0"

use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
use chrono::prelude::{Utc, SecondsFormat};
use hmac::{Hmac, Mac};
use sha2::{Sha256};

static API_KEY: &'static str = "<insert your key!>";
static API_SECRET: &'static str = "<insert your secret!>";
static PASSPHRASE: &'static str = "<insert your passphrase!>";

fn main() {
    let timestamp = Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true);
    let method = "POST";
    let request_path = "/api/spot/v3/orders";

    let body_str = "{\"type\": \"market\", \"side\": \"sell\", \"instrument_id\": \"ETH-USDT\", \"size\": \"0.001\"}";
    let mut signature_content = String::new();
    signature_content.push_str(&timestamp);
    signature_content.push_str(method);
    signature_content.push_str(request_path);
    signature_content.push_str(&body_str);

    type HmacSha256 = Hmac<Sha256>;
    let mut mac = HmacSha256::new_varkey(API_SECRET.as_bytes()).unwrap();
    mac.input(signature_content.as_bytes());
    let signature = mac.result().code();
    let base64_signature = base64::encode(&signature);

    let mut header_map = HeaderMap::new();
    header_map.insert("OK-ACCESS-KEY", HeaderValue::from_str(API_KEY).unwrap());
    header_map.insert("OK-ACCESS-SIGN", HeaderValue::from_str(&base64_signature).unwrap());
    header_map.insert("OK-ACCESS-TIMESTAMP", HeaderValue::from_str(&timestamp).unwrap());
    header_map.insert("OK-ACCESS-PASSPHRASE", HeaderValue::from_str(PASSPHRASE).unwrap());
    header_map.insert(CONTENT_TYPE, HeaderValue::from_static("application/json; charset=UTF-8"));

    let client = reqwest::Client::new();
    let mut complete_url = String::from("https://okex.com");
    complete_url.push_str(request_path);

    let res = client
        .post(complete_url.as_str())
        .headers(header_map)
        .body(body_str)
        .send().unwrap().text();

    println!("{:#?}", res);
}

此时会返回“无效签名”错误,但应返回成功的 http 代码(如果帐户中有足够的资金)。

系统管理员

解决方案是使用"https://www.okex.com"而不是"https://okex.com. 后者产生“无效符号”错误。但仅适用于 POST 请求。因此,问题与 Rust 无关。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章