R中具有身份验证的API POST [英] API POST with authentication in R

查看:88
本文介绍了R中具有身份验证的API POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对Bitmex进行POST操作,以4009.9的价格购买1张名为XBTUSD(比特币/美元)的合约,到期时间为1545674400(UNIX时间戳,从现在开始几个小时),我们需要进行身份验证.这看起来很简单.

I'm trying to make a POST action on Bitmex to buy 1 contract called XBTUSD (bitcoin/usd) at price 4009.9, with expiring time 1545674400 (UNIX timestamp, a couple hours from now) and we need to be authenticated. This looks simple.

我们正在使用API​​,并且正在用R编写.(api-secret是假的,抱歉!)

We are using API and we are writing in R. (api-secret is fake sorry!!!)

我们需要在签名中使用hmac转换我们的请求,以使其以16为底的数字.

We need to transform our request with hmac in a signature to make it a number in base 16.

我们尝试定义签名

  signature=hmac("Kjxd5H5sPnBq6oXmnKrQAbKPIAXuKsInHRmD9CF2Dh3-4I6j", 'POST/api/v1/order1545674400{"symbol":"XBTUSD","price":4009.0,"orderQty":1}', algo = "sha256")

然后到POST

POST("https://www.bitmex.com/api/v1/order",body = 'POST/api/v1/order1545674400{"symbol":"XBTUSD","price":4009.0,"orderQty":1}',add_headers("api-key":"R1IdBlJD0-fCXypR2TTQVCF6", "api-signature":signature))

或类似的东西,我们得到

or similar stuff, and we get

403或401

我不明白怎么了.我可以发出不需要身份验证的请求,但不需要身份验证的请求!

I don't understand what's wrong. I'm able make requests which don't need authentication, but not those with it!

谢谢!

推荐答案

,您似乎已经忘记了标头中的"api-expires"参数. 对我有用的方法是,该主体是一个命名列表(如果您使用的是httr包),并且在"POST"函数调用中带有附加参数:encode ='json'.

from the first glance, you seem to have forgotten 'api-expires' parameter among headers. Method that works for me is, that body is a named list (if you are using httr package) with aditional parameter inside 'POST' function call: encode = 'json'.

如果您想更详细地了解错误的含义,请执行以下操作:

If you want to see what the errors mean in more detail, do the following:

msg = POST(....) rawToChar(msg $ content)

msg = POST(....) rawToChar(msg$content)

这篇关于R中具有身份验证的API POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆