Search code examples
rapisha256hmacbinance

401 Unauthorized error Binance API in r


I am trying to create a new order via the Binance API using RStudio.

I found the Binance Official API Docs and figured out that I should use:

POST /api/v3/order (HMAC SHA256).

The following script doesn't work out and gives a status code: HTTP/1.1 401 Unauthorized

key <- "*[my key]*"
secret <- "*[my secret key]*"
url='https://api.binance.com/api/v3/order'
timestamp <- as.character(jsonlite::fromJSON(content(GET('https://api.binance.com/api/v1/time'), "text"))$serverTime + 999)
query <- list( "symbol" = "XLMBTC", "side" = "BUY", "type" = "MARKET", "quantity" = 10, "recvWindow" = 5000, "timestamp" = timestamp )
signature <- digest::hmac(key = secret, object = paste(names(query), query, sep = "=", collapse = "&"), algo = "sha256")
POST(url, add_headers("X-MBX-APIKEY"= key), query = c(query, signature = signature), verbose())

Is there a bug in my coding? I tried this once before and then it worked perfectly well. However, now I cannot come up with a working script.

Here you can find documentation about the Binance API: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md

Can anybody help me out?


Solution

  • It's already solved. Ik checked this website for the solution: https://gunbot.shop/error-response-code-400-on-binance-how-to-fix/

    The problem was that my API and SECRET key were broken.