Jump to content

API calling Binance - (Moved)


Recommended Posts

Hi,

I have tried to write a script to call Binance for Account information... just trying to get the structure right before I create the other calls. My attempt is below... but I keep on getting the response code 403 which means it is on my side malformed calling or whatever... I have remove my API key and Secret for obvious security reasons...I have left other code snippets that show other attempts... Please help!

#include <Crypt.au3>
#include <Date.au3>
#include <Array.au3>
#include <string.au3>
#include <MsgBoxConstants.au3>
#include <Timers.au3>
#include <String.au3>

$timeStamp = _Timer_Init()
ConsoleWrite("TimeStamp >> " & $timeStamp & @CRLF)

;$time = _TimeGetStamp()
;ConsoleWrite("time >> " & $time & @CRLF)

__AccountInfo()

Func __AccountInfo()
    ; https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md     site with information on Binance API calls
    $accessKey = "" ;; Add the key from Binance
    $secretKey = "" ;; Add the key from Binance
    $param = 'recvWindow=20000&timestamp=' & $timeStamp
    $BinarySignature = HMAC($secretKey, $param)
    ;$signature = _Base64Encode($BinarySignature) ;Encode signature
    $signature = _StringToHex($BinarySignature)
    ;ConsoleWrite(">> Signature >> " & $signature & @CRLF)
    $request = $param & '&signature=' & $signature
    ;ConsoleWrite(">> $request >> " & $request & @CRLF)
    $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    $oHTTP.Open("GET", "https://api.binance.com/api/v3/account", False)
    $oHTTP.SetRequestHeader("User-Agent", 'Mozilla/5.0 (Windows NT 10.0; WOW64) WinHttp/1.6.3.9 (WinHTTP/5.1) like Gecko')
    $oHTTP.SetRequestHeader("X-MBX-APIKEY", $accessKey)
    $oHTTP.Send($request)
    $oHTTP.Send()
    $oReceived = $oHTTP.ResponseText
    $oStatusCode = $oHTTP.Status
    If $oStatusCode <> 200 Then
        MsgBox(4096, "Response code", $oStatusCode)
    EndIf
    ConsoleWrite("Response Text >> " & $oReceived & @CRLF & @CRLF)
EndFunc   ;==>__BinanceAPI

Func sha256($message)
    Return _Crypt_HashData($message, $CALG_SHA_256)
EndFunc

Func HMAC($key, $message, $hash="sha256")
    Local $blocksize = 64
    Local $a_opad[$blocksize], $a_ipad[$blocksize]
    Local Const $oconst = 0x5C, $iconst = 0x36
    Local $opad = Binary(''), $ipad = Binary('')
    $key = Binary($key)
    If BinaryLen($key) > $blocksize Then $key = Call($hash, $key)
    For $i = 1 To BinaryLen($key)
        $a_ipad[$i-1] = Number(BinaryMid($key, $i, 1))
        $a_opad[$i-1] = Number(BinaryMid($key, $i, 1))
    Next
    For $i = 0 To $blocksize - 1
        $a_opad[$i] = BitXOR($a_opad[$i], $oconst)
        $a_ipad[$i] = BitXOR($a_ipad[$i], $iconst)
    Next
    For $i = 0 To $blocksize - 1
        $ipad &= Binary('0x' & Hex($a_ipad[$i],2))
        $opad &= Binary('0x' & Hex($a_opad[$i],2))
    Next
    Return Call($hash, $opad & Call($hash, $ipad & Binary($message)))
EndFunc

Func _TimeGetStamp()
    Local $av_Time
    $av_Time = DllCall('CrtDll.dll', 'long:cdecl', 'time', 'ptr', 0)
    If @error Then
        SetError(99)
        Return False
    EndIf
    Return $av_Time[0]
EndFunc

Func _Base64Encode($input)

    $input = Binary($input)

    Local $struct = DllStructCreate("byte[" & BinaryLen($input) & "]")

    DllStructSetData($struct, 1, $input)

    Local $strc = DllStructCreate("int")

    Local $a_Call = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _
            "ptr", DllStructGetPtr($struct), _
            "int", DllStructGetSize($struct), _
            "int", 1, _
            "ptr", 0, _
            "ptr", DllStructGetPtr($strc))

    If @error Or Not $a_Call[0] Then
        Return SetError(1, 0, "") ; error calculating the length of the buffer needed
    EndIf

    Local $a = DllStructCreate("char[" & DllStructGetData($strc, 1) & "]")

    $a_Call = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _
            "ptr", DllStructGetPtr($struct), _
            "int", DllStructGetSize($struct), _
            "int", 1, _
            "ptr", DllStructGetPtr($a), _
            "ptr", DllStructGetPtr($strc))

    If @error Or Not $a_Call[0] Then
        Return SetError(2, 0, ""); error encoding
    EndIf

    Return DllStructGetData($a, 1)

EndFunc   ;==>_Base64Encode

 

Edited by Jos
put source in codebox for readability
Link to comment
Share on other sites

  • 1 month later...
  • 11 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...