-
Feb 1st, 2020, 01:06 PM
#1
Thread Starter
New Member
Binance order API : The remote server returned an error: (400) Invalid command
I`m using Visual Studio 2015 version 14.0. and the Binance-official-API-docs.
https://github.com/binance-exchange/...icial-api-docs
I try to make an order API for Binance but receive an exception 400.
The code I got from the topic below works perfect, so my API key`s are ok.
http://www.vbforums.com/showthread.p...to-Binance-API
Code:
Public Function HashString(ByVal StringToHash As String, ByVal HachKey As String) As String
Dim myEncoder As New System.Text.UTF8Encoding
Dim Key() As Byte = myEncoder.GetBytes(HachKey)
Dim Text() As Byte = myEncoder.GetBytes(StringToHash)
Dim myHMACSHA1 As New System.Security.Cryptography.HMACSHA1(Key)
Dim HashCode As Byte() = myHMACSHA1.ComputeHash(Text)
Dim hash As String = Replace(BitConverter.ToString(HashCode), "-", "")
Return hash.ToLower
End Function
Private Sub PostOrder(BuyOrSell As String)
Dim TimeStamp As String = CLng((DateTime.UtcNow - #1970/01/01#).TotalMilliseconds).ToString
Dim TotalParam As String = "timestamp=" + TimeStamp
Dim HashKey As String = HashString(TotalParam, SecretKey)
Dim APIUrl As String = "https://api.binance.com/api/v3/account?" + TotalParam + "&signature=" + HashKey
Dim Request As System.Net.HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(APIUrl), System.Net.HttpWebRequest)
Request.Headers.Add("X-MBX-APIKEY", APIKey)
Dim Response As System.Net.HttpWebResponse = DirectCast(Request.GetResponse(), System.Net.HttpWebResponse)
Dim Read = New System.IO.StreamReader(Response.GetResponseStream).ReadToEnd
MsgBox(Read)
End Sub
When I try to send an order API and some parameters with it I get the exception "The remote server returned an error: (400) Invalid command".
Code:
Dim TimeStamp As String = CLng((DateTime.UtcNow - #1970/01/01#).TotalMilliseconds).ToString
Dim QueryString As String = "symbol=" & symbol & "&side=" & BuyOrSell & "&type=LIMIT&timeInForce=GTC"
Dim RequestBody As String = "quantity=" & Quantity & "&price=" & askPrice & "&recvWindow=" & RecvWindow & "×tamp=" + TimeStamp
Dim TotalParam As String = QueryString + RequestBody
Dim HashKey As String = HashString(TotalParam, SecretKey)
Dim APIUrl As String = "https://api.binance.com/api/v3/order?" + QueryString + "&" + RequestBody + "&signature=" + HashKey
The URL I send :
https://api.binance.com/api/v3/order...e=.....HashKey.....
I don`t know where I made the mistake in my API.
-
Feb 1st, 2020, 06:55 PM
#2
Re: Binance order API : The remote server returned an error: (400) Invalid command
Isn't there an ampersand missing between the two parts of the param string you are assembling to use in creating the HMAC signature?:
Code:
Dim TotalParam As String = QueryString + RequestBody
You do add it when forming the URL:
Code:
Dim APIUrl As String = "https://api.binance.com/api/v3/order?" + QueryString + "&" + RequestBody + "&signature=" + HashKey
-
Feb 2nd, 2020, 05:23 AM
#3
Thread Starter
New Member
Re: Binance order API : The remote server returned an error: (400) Invalid command
I put in some code to get a read of the webresponse.
The real error message Binance returned is "code":-1022,"msg":"Signature for this request is not valid."
So when I put in my parameters it goes wrong.
Code:
Try
' Here the code I showed befor
Using myWebResponse As WebResponse = Request.GetResponse()
Using myResponseStream As Stream = myWebResponse.GetResponseStream()
Using myStreamReader As StreamReader = New StreamReader(myResponseStream)
myStreamReader.ReadToEnd()
MsgBox(myStreamReader)
End Using
End Using
End Using
Catch ex As WebException
Console.WriteLine(ex.Status)
If ex.Response IsNot Nothing Then
'' can use ex.Response.Status, .StatusDescription
If ex.Response.ContentLength <> 0 Then
Using stream = ex.Response.GetResponseStream()
Using reader = New StreamReader(stream)
Dim ReaderResult As String
ReaderResult = reader.ReadToEnd()
Console.WriteLine(ReaderResult)
MsgBox(ReaderResult)
End Using
End Using
End If
End If
End Try
When I put the ampersand like "Dim TotalParam As String = QueryString + "&" + RequestBody"
I get the message "code":-1013,"msg":"Filter failure: MIN_NOTIONAL"
-
Feb 2nd, 2020, 05:41 AM
#4
Thread Starter
New Member
Re: Binance order API : The remote server returned an error: (400) Invalid command
Inferrd You where right.
After I put in the ampersand I got the message "code":-1013,"msg":"Filter failure: MIN_NOTIONAL".
This means Binance MIN_NOTIONAL Error (Purchase / Sell too small?)
So I raised the quantity, then I got a perfect respons.
Thanks for your great help.
-
Sep 1st, 2020, 04:59 PM
#5
New Member
Re: Binance order API : The remote server returned an error: (400) Invalid command
Dim TimeStamp As String = CLng((DateTime.UtcNow - #1970/01/01#).TotalMilliseconds).ToString
why date constant is not valid? then part #1970/01/01#
-
Jun 17th, 2021, 01:33 PM
#6
New Member
Re: Binance order API : The remote server returned an error: (400) Invalid command
Hi Steef69,
I am struggling with the same issue. I keep getting error400.
I use the same hashstring function as you. My code for placing the order is :
Dim TimeStamp As String
TimeStamp = CLng((DateTime.UtcNow - #1970/01/01#).TotalMilliseconds).ToString
Try
Dim TotalParam As String
TotalParam = "symbol=BTC_USDT&side=SELL&type=LIMIT&timeInForce=GTC&quantity=5&price=1&recvWindow=5000×ta mp=" & TimeStamp
Dim HashKey As String
HashKey = HashString(TotalParam, SecretKey)
Dim APIUrl As String
APIUrl = "https://api.binance.com/api/v3/order/test?" + TotalParam + "&signature=" + HashKey
Dim Request As System.Net.HttpWebRequest
Request = DirectCast(System.Net.HttpWebRequest.Create(APIUrl), System.Net.HttpWebRequest)
'Request.Method = "POST"
Request.Headers.Add("X-MBX-APIKEY", APIKey)
Request.ContentType = "application/x-www-form-urlencoded"
Dim Response As System.Net.HttpWebResponse = DirectCast(Request.GetResponse(), System.Net.HttpWebResponse) '
Dim Read = New System.IO.StreamReader(Response.GetResponseStream).ReadToEnd
rtxtReply.Text = Read
rtxtReply.Refresh()
Catch ex As Exception
rtxtReply.Text = "ERROR: " & ex.ToString
End Try
But for some reason I cant get it working.
Could you help me out please?
Thanks in advance !
Robin
-
Jun 18th, 2021, 06:49 PM
#7
Re: Binance order API : The remote server returned an error: (400) Invalid command
There's a few things wrong that I can see:
- There's an error in the HashString Function in Post #1. It should be using a HMACSHA256 algorithm, not HMACSHA1.
- Your TotalParam query string has an unwanted space in ×ta mp=".
- Also in the query string, BTC_USDT is not a valid symbol.
- The Price you are selling at is very generous, but will probably cause the API to bork.
- You need to set Request.Method = "POST", so uncomment that line.
Also, look at the way the exception handling is performed in Post #3. When there's an error, the server often returns a response that gives clues as to what caused the error. The WebException handler code in Post #3 shows you how to pull the message out of that error response.
-
Jun 19th, 2021, 01:27 AM
#8
New Member
Re: Binance order API : The remote server returned an error: (400) Invalid command
Thank you Inferrd ! The space and symbol were indeed copy paste errors from all my testing. However, the api was blocking because of the price which is checked even in test mode. And I also had to uncomment the post.
Solved! Many thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|