Results 1 to 2 of 2

Thread: Binance API and VS2019 using VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    4

    Binance API and VS2019 using VB

    Not only am I new to this forum, but also new to Crypto
    I am trying to connect to Binance API, but after trying several methods, I am lost.
    I can get the quotes without issue, as they are basically open to anyone. Where the problem comes is when I try to get to a signed API endpoint.

    This is the latest iteration of my code: (not working)
    Code:
    Dim RealURL As String = "https://api.binance.com"
    Const WalletURL As String = "/sapi/v1/capital/config/getall"
    
    Function getWalletInfo() As String
            Dim URL = New UriBuilder(RealURL & WalletURL)
            Dim TimeStamp As String = CLng((DateTime.UtcNow - #1970/01/01#).TotalMilliseconds).ToString
            Dim HashKey As String = getHash(BinanceSecret)
            Dim queryString = HttpUtility.ParseQueryString(String.Empty)
            Dim client = New WebClient()
            client.Headers.Add("X-MBX-APIKEY", BinanceKey)
            client.Headers.Add("content-type", "application/x-www-form-urlencoded")
            queryString("signature") = HashKey
            queryString("timestamp") = GetTimeStamp()
            URL.Query = queryString.ToString()
            Dim result As String
            Try
                result = client.DownloadString(URL.ToString)
                'Read_JSon(result)
            Catch ex As Exception
                result = ""
                'Stop
            End Try
    End Function
    
    Function getHash(myKey As String) As String
            Dim TimeStamp As String
            TimeStamp = CLng((DateTime.UtcNow - #1970/01/01#).TotalMilliseconds).ToString
            Dim TotalParam As String
            TotalParam = "timestamp=" + TimeStamp
            Dim keyBytes As Byte() = Encoding.UTF8.GetBytes(myKey)
            Dim hmacsha256 = New HMACSHA256(keyBytes)
            Dim sourceBytes As Byte() = Encoding.UTF8.GetBytes(TotalParam)
            Dim hash As Byte() = hmacsha256.ComputeHash(sourceBytes)
            Return BitConverter.ToString(hash).Replace("-", "").ToLower()
        End Function
    BinanceKey and BinanceSecret are variables defined elsewhere

    With all my various attempt, I either get unauthorized, bad request or just server error 400, as this code does.
    The URL "Looks" correct, but I am lost as to why I can't get anywhere.
    Last edited by si_the_geek; Aug 14th, 2022 at 05:17 PM. Reason: added Code tags

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    4

    Re: Binance API and VS2019 using VB

    Finally got it to work
    The hash code seems only need the string "timestamp=" & timestamp and the timestamp = TimeStamp = CLng((DateTime.UtcNow - #1970/01/01#).TotalMilliseconds).ToString

    In the code I was looking at, it had totalparameters as the requirement for the hash, so naturally I used ALL of the parameters, WRONG!

    Here is the code that got me into actually using the API for Binance:

    'The BinanceKey and BinanceSecret are variables (left out for obvious reasons)

    Dim TestURL As String = "https://testnet.binance.vision"
    Dim RealURL As String = "https://api.binance.com"
    Const WalletEndPoint As String = "/sapi/v1/capital/config/getall"

    Function getHash(timestamp As String) As String
    Dim keyBytes As Byte() = Encoding.UTF8.GetBytes(BinanceSecret)
    Dim hmacsha256 = New HMACSHA256(keyBytes)
    Dim sourceBytes As Byte() = Encoding.UTF8.GetBytes("timestamp=" & timestamp)
    Dim hash As Byte() = hmacsha256.ComputeHash(sourceBytes)
    Return BitConverter.ToString(hash).Replace("-", "").ToLower()
    End Function

    Function getWalletInfo() As String
    Dim URL = New UriBuilder(RealURL & WalletEndPoint)
    Dim TimeStamp As String = CLng((DateTime.UtcNow - #1970/01/01#).TotalMilliseconds).ToString
    Dim HashKey As String = getHash(TimeStamp)
    Dim queryString = HttpUtility.ParseQueryString(String.Empty)
    Dim client = New WebClient()
    client.Headers.Add("X-MBX-APIKEY", BinanceKey)
    queryString("timestamp") = TimeStamp
    queryString("signature") = HashKey
    URL.Query = queryString.ToString()
    Dim result As String
    Try
    result = client.DownloadString(URL.ToString)
    'Read_JSon(result)
    Catch ex As Exception
    result = ""
    'Stop
    End Try
    Return result
    End Function

    This actually works!
    The return JSON string is something else, just tons of data, most of which has nothing to do with my wallet. But at least it does work on my account.

    A word of warning:
    When you set up your API key, make absolutely sure that you copy the Secret Key because you can never go back and get it. At least I have not found how you can retrieve it. The API key is always there on the site.
    Last edited by Sjmay; Aug 15th, 2022 at 01:15 PM.

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