dcsimg
Results 1 to 4 of 4

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,282

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width