Results 1 to 8 of 8

Thread: Binance order API : The remote server returned an error: (400) Invalid command

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    3

    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 & "&timestamp=" + 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.

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    3

    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"

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    3

    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.

  5. #5
    New Member
    Join Date
    Sep 2020
    Posts
    1

    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#

  6. #6
    New Member
    Join Date
    Jun 2021
    Posts
    2

    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&timesta 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

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    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 &timesta 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.

  8. #8
    New Member
    Join Date
    Jun 2021
    Posts
    2

    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
  •  



Click Here to Expand Forum to Full Width