R Binance API HMAC SHA256签名消息

时间:2018-01-26 04:54:06

标签: r api post sha256 hmac

我试图使用binance API发送已签名的api消息,但我仍然遇到404错误。有人可以帮我解决下面的代码吗?

library(jsonlite)
library(httr)
library(dplyr)
library(digest)


timestamp <- 1516941586 #as.numeric(as.POSIXct(Sys.time()))

post_message <- paste0(timestamp, 'public.api' ) # data_client.id = client 
id # data_key = key

sha.message <- toupper(digest::hmac('private.api', object = post_message, 
algo = 'sha256', serialize = F))

url <- 'https://api.binance.com/api/v3/account'

body = list('timestamp' = timestamp, 'signature' = sha.message)
body2 <- paste("?timestamp=",timestamp,"&signature=",sha.message, sep = "")


httr::POST(url, body2 = body, verbose())

这是文档https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md

1 个答案:

答案 0 :(得分:0)

根据网站上“POST / api / v1 / order的签名端点示例”部分的示例,您可以遵循类似的内容。您需要使用自己的apiKey和secretKey替换。

library(httr)
library(openssl)

url <- 'https://api.binance.com/api/v3/account'
apiKey <- "vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A"
secretKey <- "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"

timestamp <- 1516941586
recvWindow <- 1e20
postmsg <- paste0("timestamp=", timestamp, "&recvWindow=", recvWindow)
signature <- openssl::sha256(postmsg, key=secretKey)

GET(url, 
    add_headers("X-MBX-APIKEY"=apiKey),
    query=list(timestamp=timestamp, recvWindow=recvWindow, signature=signature), 
    verbose())