dcsimg
Results 1 to 3 of 3

Thread: How to connect to Binance API?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    2

    Question How to connect to Binance API?

    this is the my code, but it aways returns "400" error, how is the right way to call the api and authenticate the connection?

    Code:
            Dim TimeStamp As String = DateTime.Now.Ticks.ToString
            Dim TotalParam As String = "timestamp=" + TimeStamp
            Dim HashKey As String = HashString(SecretKey, TotalParam)
    
            Dim APIUrl As String = "https://api.binance.com/api/v3/account?" + TotalParam + "&signature=" + HashKey
    
            Dim Request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(APIUrl)
            Request.Headers.Add("X-MBX-APIKEY", ApiKey)
    
            Dim Response As System.Net.HttpWebResponse = Request.GetResponse()
            Dim Read = New System.IO.StreamReader(Response.GetResponseStream).ReadToEnd
    
            MsgBox(Read)
    and here is my hashstring code:

    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
    Last edited by playdachi; Sep 13th, 2019 at 04:29 PM.

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

    Re: How to connect to Binance API?

    Hi playdachi, welcome to the forum.

    You have one or two mistakes in your code:

    The timestamp should be a Unix millisecond timestamp (count of milliseconds elapsed since 1st January, 1970).

    You have mixed up the order of the arguments you are passing to your HashString function.

    You should be using HMAC SHA256 to generate your signature, not HMAC SHA1.


    So your code with corrections would be:
    Code:
        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)
    
    
    
    
    
    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 myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
        Dim HashCode As Byte() = myHMACSHA256.ComputeHash(Text)
        Dim hash As String = Replace(BitConverter.ToString(HashCode), "-", "")
        Return hash.ToLower
    End Function

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    2

    Re: How to connect to Binance API?

    Quote Originally Posted by Inferrd View Post
    Hi playdachi, welcome to the forum.

    You have one or two mistakes in your code:

    The timestamp should be a Unix millisecond timestamp (count of milliseconds elapsed since 1st January, 1970).

    You have mixed up the order of the arguments you are passing to your HashString function.

    You should be using HMAC SHA256 to generate your signature, not HMAC SHA1.


    So your code with corrections would be:
    Code:
        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)
    
    
    
    
    
    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 myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
        Dim HashCode As Byte() = myHMACSHA256.ComputeHash(Text)
        Dim hash As String = Replace(BitConverter.ToString(HashCode), "-", "")
        Return hash.ToLower
    End Function
    It works! thank u very much, now i dont need to use a nuget package for do it, thank u very much!

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