krijnsent / crypto_vba

An Excel/VBA project to communicate with various cryptocurrency exchanges APIs
MIT License
155 stars 54 forks source link

May I ask you for some advice regarding to POST? #30

Closed thawatchai-w closed 6 years ago

thawatchai-w commented 6 years ago

First of all thank you for your work. I have been working on your vba in order to get one of my exchange account running. However, there is no luck so far i get lost on how to send POST form data. I sent the request and get this error. {"success":false,"error":"You did not set any API key. Make sure you send your request as POST form-data, and not as GET or JSON body request."} The document is in english and in this link https://bx.in.th/info/api/ If you have a bit time your help would be very appreciate. Thank you in advance

Below is the code

Function PrivateBX(Method As String, apikey As String, secretkey As String, Optional MethodOptions As Collection) Dim NonceUnique As String Dim Json As String Dim PayloadDict As Scripting.Dictionary Dim twofa As String Dim temp As String Dim strResult As String

twofa = ""

NonceUnique = CreateNonce(15) temp = apikey + NonceUnique + secretkey ApiSite = "https://bx.in.th/api/" Signature = ComputeHash_C("SHA256", temp, "", "byte") Url = ApiSite + Method + "/" Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") objHTTP.Open "POST", Url, False objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" objHTTP.send ("apikey=apikey & nonce=NonceUnique & signature=Signature & twofa=twofa") objHTTP.send (postdata) objHTTP.WaitForResponse PrivateBX = objHTTP.responseText Set objHTTP = Nothing

End Function

krijnsent commented 6 years ago

Hi, I'll add it to my to-do list, but you'll have to do the testing as I don't have an account with them. Will hopefully somewhere in the coming week.

krijnsent commented 6 years ago

Hi @thawatchai-w I had a quick try, but the public API of that exchange doesn't work (it's blocked by a captcha test) so I decided not to include it into my project for now. Secondly, next time do tell if you're cross-posting your issue...

For your code, the main issues I can spot: temp = apikey & NonceUnique & secretkey '(not using + but &)

And here too (VBA won't replace this as PHP could do): objHTTP.send ("key=" & apikey & "&nonce=" & NonceUnique & "&signature=" & Signature & "&twofa=" & twofa)

And remove the objHTTP.send (postdata) line.

Finally, Signature = ComputeHash_C("SHA256", temp, "", "byte") -> I guess you'd need "STRHEX" as the last option of that function, not "byte".

That should hopefully improve things/get you another error message. Please close this issue if it's solved.

thawatchai-w commented 6 years ago

Hi @krijnsent , First of all thank you for your help a lot. I managed to get it done. Thxs Following is the working code. Function PrivateBX(Method As String, apikey As String, secretkey As String)

Dim NonceUnique As String Dim Json As String Dim PayloadDict As Scripting.Dictionary Dim twofa As String Dim temp As String Dim strResult As String

twofa = ""

NonceUnique = CreateNonce(2) temp = apikey & NonceUnique & secretkey ApiSite = "https://bx.in.th/api/" Signature = ComputeHash_C("SHA256", temp, "", "STRHEX") Url = ApiSite + Method + "/" Debug.Print apikey Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") objHTTP.Open "POST", Url, False objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" objHTTP.send ("key=" & apikey & "&nonce=" & NonceUnique & "&signature=" & Signature & "&twofa=" & twofa) objHTTP.WaitForResponse PrivateBX = objHTTP.responseText Set objHTTP = Nothing

End Function