BTCMarkets / API

API
120 stars 30 forks source link

Post request always reply - Authentication failed #95

Closed sigulete closed 6 years ago

sigulete commented 6 years ago

I was successful when using GET and obtaining the balances of my wallets, but I'm unable to use the POST request to create an order. I have reviewed the C++ example in GitHub and I can't see any error, but it still doesn't work.

Data: {\"currency\":\"AUD\",\"instrument\":\"BTC\",\"price\":1600000000000,\"volume\":100000000, \"orderSide\":\"Bid\",\"ordertype\":\"Limit\",\"clientRequestId\":\"1\"} Message to sign: "/order/create"+"\n"+timespan+"\n"+Data Header:

"Content-Type","application/json" -- ONLY with POST request
"Content-Length", Data.size() -- ONLY with POST request
"Accept","application/json"
"Accept-Charset","UTF-8"
"apikey", ApiKey
"timestamp", timespan
"signature", signature

What can be the problem?? All the above works with GET, but not with POST.

meeeee12 commented 6 years ago

try removing the backslashes from your data string

sigulete commented 6 years ago

Back slashes cannot be removed. Otherwise the apostrophes " wouldn't be allowed. They are acting as escape code.

I have tried removing both \" from Data, but the server replied NULL.

plourenco75 commented 6 years ago

I am having the same issue with nodejs call to create order. What is interesting is that the same code "worked" one time .. ie the buy order was successful. Then running the same code with a new price bid, and I always get Authentication Failed.

$ node testbuys.js my AUD balance is: 5203004203 trying to buy 2.3001786927497787 XRP for 3.77

jse_shortmsg: 'BTCMarkets.executeRequest() failed POST request to url https://api.btcmarkets.net/order/create with message /order/create\n1515393520484\n{"currency":"AUD","instrument":"XRP","price":377000000,"volume":230017869.27497786,"orderSide":"Bid","ordertype":"Market","clientRequestId":"SSPL_696969"}. Error message: Authentication failed.', jse_summary: 'BTCMarkets.executeRequest() failed POST request to url https://api.btcmarkets.net/order/create with message /order/create\n1515393520484\n{"currency":"AUD","instrument":"XRP","price":377000000,"volume":230017869.27497786,"orderSide":"Bid","ordertype":"Market","clientRequestId":"SSPL_696969"}. Error message: Authentication failed.', message: 'BTCMarkets.executeRequest() failed POST request to url https://api.btcmarkets.net/order/create with message /order/create\n1515393520484\n{"currency":"AUD","instrument":"XRP","price":377000000,"volume":230017869.27497786,"orderSide":"Bid","ordertype":"Market","clientRequestId":"SSPL_696969"}. Error message: Authentication failed.', name: 'Authentication failed.' } { success: false, errorCode: 1, errorMessage: 'Authentication failed.' }

Please help?

meeeee12 commented 6 years ago

@lusito75 you cannot have decimals points in your volume, round to the nearest number. So you will end up buying 2.30017869 xrp

meeeee12 commented 6 years ago

@sigulete try encapsulating in other quote type: '{"currency":"AUD","instrument":"BTC","price":1600000000000,"volume":100000000, "orderSide":"Bid","ordertype":"Limit","clientRequestId":"1"}'

bootylicious1 commented 6 years ago

I think the problem might be that you are missing information in your request.

Try adding a "BodyData" parameter and setting it equal to request string (eg. "{\"currency\":\"AUD\",\"instrument\":\"BTC\",\"price\":1600000000000,\"volume\":100000000, \"orderSide\":\"Bid\",\"ordertype\":\"Limit\",\"clientRequestId\":\"1\"}"

sigulete commented 6 years ago

@meeeee12 I have tried to encapsulate with single quotes ' ', but it didn't work. The gpp compiler interprets as single character and only '{' is captured. @bootylicious1 The BodyData is part of the POST request.

The Data string seems to be correct, because any other way to write the JSON string and the server reply NULL. The only problem is that it keeps responding "Authentication failed"

bootylicious1 commented 6 years ago

I understand that the "BodyData" information is already contained in the signature. What I am saying is that it might also be necessary to state it elsewhere in the function.

Using Mathematica, I did not have to include this in the GET request in order to get authentication. But for the POST request, it would not authenticate without it. You don't put it in the {Accept, accept-charset, content-type, apikey, timestamp, signature} part. That should only contain those 6 things in that order. You have to add the "BodyData" part somewhere else in the function. Maybe.

Another thing you could try is setting the post request to pass the {Accept, accept-charset, content-type, apikey, timestamp, signature} part as "Headers" and not "Parameters". For me, the I could use both for successful GET requests, but for successful POST requests this information needed to be parsed as "Headers".

bootylicious1 commented 6 years ago

And check if the signatures you are generating are correct.

To check, make sure your script generates the same signatures as the ones generated by the site https://www.liavaag.org/English/SHA-Generator/HMAC/ (setting TEXT input, TEXT key, SHA-512 variant, and BASE-64 output). Then, after you are generating the exact same ones, change your script so that it uses a Base-64 Decoded version of the private key.

ie. if your private key is:

0000000000000000000000000000000000000000000000000000000000000000000000\ 000000000000000000

The Base-64 Decoded key your script should be using is:

ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4ÓM4

justin-ngin commented 6 years ago

Hello @sigulete,

Could you please attach a code snippet of the entire post request object that you are building and sending?

Thanks, Justin

sigulete commented 6 years ago

I found the issue. When parsing the Data: {\"currency\":\"AUD\",\"instrument\":\"BTC\",\"price\":1600000000000,\"volume\":100000000, \"orderSide\":\"Bid\",\"ordertype\":\"Limit\",\"clientRequestId\":\"1\"}

The price was being taken from a variable of type double and the number was being inserted in scientific notation 1.6e+12. So, I just made the number explicitly of type long.

Change: \"price\":1600000000000 for \"price\":(long)1600000000000

And problem solved.