简体   繁体   中英

How to properly make POST request

I'm trying to make my first POST request to an API. For some reason, I always get status 403 in return. I suspect it's the signature that is being incorrectly generated. The api-key and client id is for sure correct.

My code

nonce <-as.integer(Sys.time())

post_message <- paste0(nonce, data_client.id, data_key) # data_client.id = client id # data_key = key

sha.message <- toupper(digest::hmac(data_secret, object = post_message, algo = 'sha256', serialize = TRUE))

url <- 'https://www.bitstamp.net/api/v2/balance/'


body = list('API-KEY' = data_key, 'nonce' = nonce, 'signature' = sha.message)

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

Output

<- HTTP/1.1 403 Authentication Failed

I'm trying to access the Bitstamp API: https://www.bitstamp.net/api/?package=Rbitcoin&version=0.9.2

All private API calls require authentication. For a successful authentication you need to provide your API key, a signature and a nonce parameter.

API KEY

To get an API key, go to "Account", "Security" and then "API Access". Set permissions and click "Generate key".

NONCEN

once is a regular integer number. It must be increased with every request you make. Read more about it here. Example: if you set nonce to 1 in your first request, you must set it to at least 2 in your second request. You are not required to start with 1. A common practice is to use unix time for that parameter.

SIGNATURE

Signature is a HMAC-SHA256 encoded message containing nonce, customer ID (can be found here) and API key. The HMAC-SHA256 code must be generated using a secret key that was generated with your API key. This code must be converted to it's hexadecimal representation (64 uppercase characters).

I'm not sure if your question is still standing, but based on your code, I managed to get it working. In fact, the main problem is in the body, the API documentation shows it expects 'key' instead of 'API-KEY'. Also, serialize should be FALSE instead of TRUE.

At the moment this works (but the API may change):

nonce <-as.integer(Sys.time())

post_message <- paste0(nonce, data_client.id, data_key) # data_client.id = client id # data_key = key

sha.message <- toupper(digest::hmac(data_secret, object = post_message, algo = 'sha256', serialize = FALSE))

url <- 'https://www.bitstamp.net/api/v2/balance/'

body = list('key' = data_key, 'nonce' = nonce, 'signature' = sha.message)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM