I have written a simple program to get data from my binance account.
When I run the attached program, the binance server responds:
The above program is based on this function written in php. This function works correctly.
The ApiKey and ApiSecret included in the program are valid (in case you want to run the program).
I attach the source code
When I run the attached program, the binance server responds:
This is the program:code":-2015,"msg":"Invalid API-key, IP, or permissions for action.
B4X:
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private TextAreaOut As TextArea
Private ButtonGet As Button
Private BinanceApiUrl As String
Private ApiSecret As String
Private ApiKey As String
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show
BinanceApiUrl = "https://api.binance.com/api/"
ApiKey = "VmcKfcJ127jpeGUPAKXnHXJBTSF9iMfHUqptwEb1TCvSLwgNnCJ2BuWsdVwyhsbD"
ApiSecret = "xTFh2bqOydbMumZmWcV13ecPYFTHmZa5pg0Xp3rlay5Tyog2VBoEAJQToMURhU1E"
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
'Function in charge of making requests to the Binance API
private Sub SendCommand(work As String, uri As String, Signature As Boolean)
Try
'Si se debe firmar
If (Signature == True)Then
Dim timestamp As Long
timestamp = DateTime.now 'Creamos el timestamp
Dim queryString As String
queryString = "timestamp=" & timestamp 'Creamos el Query string
Dim Sign() As Byte
Sign = HashHmac(queryString, ApiSecret) 'Firmamos el Query string
Dim API_Signed As String
Dim Byte_Conv As ByteConverter
API_Signed = Byte_Conv.HexFromBytes(Sign) 'convert to HEX
API_Signed = API_Signed.ToLowerCase
End If
Dim j As HttpJob
j.Initialize(work, Me)
Dim Url As String
Url = BinanceApiUrl & uri & "?" & queryString & "&signature=" & API_Signed 'Creamos la Url
j.Download(Url)
Log(Url)
'Si se debe firmar
If (Signature == True)Then
Try
'Defino el SetHeader basado función "signedRequest()" que encontrará en este post.
j.GetRequest.SetHeader("User-Agent", "Mozilla/4.0 (compatible; PHP Binance API)")
j.GetRequest.SetHeader("X-MBX-ApiKey", ApiKey)
j.GetRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded")
Catch
Log(LastException)
End Try
End If
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
TextAreaOut.Text = j.JobName & CRLF
TextAreaOut.Text = TextAreaOut.Text & j.GetString
Log(j.GetString)
Else
TextAreaOut.Text = j.ErrorMessage
End If
j.Release
Catch
MessageBox(LastException, "Error")
End Try
End Sub
Sub ButtonGet_Click
SendCommand("GetBalances", "v3/account", True)
End Sub
'Function in charge of Signing the QueryString
Public Sub HashHmac(data As String, secret As String) As Byte()
Try
Dim m As Mac 'm As Message Authentication Code
Dim kg As KeyGenerator 'kg As KeyGenerator
kg.Initialize("HmacSHA512") 'initialize kg using HmacSHA512 algorithm
kg.KeyFromBytes(secret.GetBytes("UTF8")) 'encode string "secret" to an array of Bytes using UTF8
m.Initialise("HmacSHA512", kg.Key) 'initialize m using HmacSHA512 algorithm and the secret key
m.Update(data.GetBytes("UTF8")) 'encodes post data to an array of Bytes and loads it to be signed
Return m.Sign 'sign the loaded data using the secret key, return the calc signature data
Catch
MessageBox(LastException, "Error")
End Try
End Sub
Public Sub MessageBox(Message As String, Title As String )
Private jxui As XUI
jxui.MsgboxAsync(Message, Title)
End Sub
The above program is based on this function written in php. This function works correctly.
PHP:
private function signedRequest($url, $params = [], $method = "GET") {
$params['timestamp'] = number_format(microtime(true)*1000,0,'.','');
$query = http_build_query($params, '', '&'); // https://www.php.net/manual/es/function.http-build-query.php
$signature = hash_hmac('sha256', $query, $this->api_secret);
$opt = [
"http" => [
"method" => $method,
"ignore_errors" => true,
"header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\nX-MBX-APIKEY: {$this->api_key}\r\nContent-type: application/x-www-form-urlencoded\r\n"
]
];
if ( $method == 'GET' ) {
// parameters encoded as query string in URL
$endpoint = "{$this->base}{$url}?{$query}&signature={$signature}";
} else {
// parameters encoded as POST data (in $context)
$endpoint = "{$this->base}{$url}";
$postdata = "{$query}&signature={$signature}";
$opt['http']['content'] = $postdata;
}
$context = stream_context_create($opt); // https://www.php.net/manual/es/function.stream-context-create.php
return json_decode(file_get_contents($endpoint, false, $context), true); // https://www.php.net/manual/es/function.json-decode.php
}
The ApiKey and ApiSecret included in the program are valid (in case you want to run the program).
I attach the source code
Attachments
Last edited: