在R中构建Exmo API认证请求时nonce格式错误(40007)求助
Fixing "Incorrect format parameter 'nonce'" Error for Exmo API in R
Let's break down why you're hitting that 40007: Incorrect format parameter 'nonce' error and fix it step by step.
First, let's go over the key mistakes in your original code:
- Nonce is in the wrong place: Exmo expects
nonceto be part of the request body (form data), not a request header. - Sign header is hardcoded: You set
Sign = "secret key"instead of using the generated HMAC signature. - Signature in query params: You added the signature to the
queryargument, but Exmo doesn't expect it there—authentication uses theSignheader instead. - Nonce type issue: Using
as.numeric(as.POSIXct(Sys.time()))gives a timestamp with decimals, but Exmo requires an integer value for nonce.
Here's the corrected code that should work:
# Load required packages (make sure they're installed first) library(httr) library(digest) library(jsonlite) # Your API credentials (replace with your actual keys) public_key <- "your_public_key_here" secret_key <- "your_secret_key_here" # Generate integer nonce (Exmo requires integer timestamp) nonce <- as.integer(Sys.time()) # Create the request body parameters (nonce is mandatory for authenticated requests) body_params <- list(nonce = nonce) # Generate HMAC-SHA512 signature # The string to sign is the URL-encoded form of the body parameters string_to_sign <- paste(names(body_params), body_params, sep = "=", collapse = "&") signature <- hmac(key = secret_key, object = string_to_sign, algo = "sha512") # Send POST request to Exmo's user_info endpoint acc <- POST( url = "https://api.exmo.com/v1/user_info/", add_headers( Key = public_key, Sign = signature ), body = body_params, encode = "form" # This ensures the body is sent as x-www-form-urlencoded ) # Parse the response get_exmo <- content(acc, as = "text") get_exmo_json <- fromJSON(get_exmo) # Check the result print(get_exmo_json)
Let's verify the critical fixes:
- Nonce as integer:
as.integer(Sys.time())gives a whole number timestamp, which matches Exmo's requirement. - Nonce in request body: We pass
noncevia thebodyargument withencode = "form", which is how Exmo expects authenticated parameters. - Correct Sign header: The generated
signatureis used here instead of a hardcoded string. - No extra query params: We don't pass signature in the URL query—authentication is handled via headers and body.
If you still run into issues, double-check that your public/secret keys are correct, and ensure the nonce value is always increasing (each subsequent request needs a higher nonce than the last).
内容的提问来源于stack exchange,提问作者Jaap
相关产品推荐
相关产品推荐

