在 R 中使用身份验证的 API POST [英] API POST with authentication in R

查看:24
本文介绍了在 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.

我们尝试定义我们的签名

We try to define our signature

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

然后发布

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天全站免登陆