使用MEXC Spot V3 API查询账户信息时持续返回400错误求助
Fix for MEXC Spot V3 API 400 Error
The 400 error occurs because your request is missing the required signature parameter for this private API endpoint. MEXC requires all authenticated requests to include a HMAC-SHA256 signature generated from the query string using your secret key. Here's how to resolve the issue:
Required Changes
- Generate an integer timestamp (no decimal places)
- Compute the HMAC-SHA256 signature of the query string
- Append the signature to the request URL
- Use the
digestpackage to handle signature generation
Corrected Code
library(httr) library(digest) access.key = "###" secret.key = "###" # Generate integer timestamp in milliseconds (no decimals) timestamp <- as.character(round(as.numeric(Sys.time()) * 1000)) # Create query string to sign query_string <- paste0("timestamp=", timestamp) # Compute HMAC-SHA256 signature signature <- hmac(key = secret.key, object = query_string, algo = "sha256", raw = FALSE) # Build full URL with timestamp and signature full.url <- paste0("https://api.mexc.com/api/v3/account?", query_string, "&signature=", signature) headers <- c( "X-MEXC-APIKEY" = access.key, "Content-Type" = "application/json" ) test_get <- httr::GET(full.url, config = add_headers(headers)) # Check response details cat("Status Code:", test_get$status_code, "\n") cat("Response Content:", content(test_get, "text"), "\n")
Key Explanations
- Integer Timestamp: MEXC expects timestamps as integers (no decimal points). Using
round()ensures you don't send invalid values like1620000000.123which would fail validation. - Signature Generation: The signature is created by hashing the query string (only the
timestamp=XXXsegment) with your secret key using HMAC-SHA256. This is mandatory for all private endpoints. - URL Construction: The signature must be added as a query parameter to the request URL for authentication.
Additional Checks
- Confirm your API key has Spot Account Read permissions enabled in your MEXC account settings.
- Ensure your system clock is synchronized (timestamps must be within 5 seconds of MEXC's server time to avoid "timestamp expired" errors).
内容的提问来源于stack exchange,提问作者Nick Amato
相关产品推荐
相关产品推荐

