Spanish Autenticación php en b4a [SOLUCIONADO]

carlos7000

Well-Known Member
Licensed User
Longtime User
Hola a todos.

Estoy tratando de crear una aplicación que se comunique con la pagina de Bittrex por medio de su api y así poder obtener datos de diferentes monedas, saber mi saldo, etc

Deseo autenticarme desde ba4. Dicha pagina da un ejemplo de autenticación, pero en php. :(

Este es el código:

B4X:
$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);

Las primeras lineas no tienen problemas. pero necesito una serie de funciones en b4a que den el mismo resultado que si tratara de autenticarme desde una aplicación en php.

Les agradecería enormemente su ayuda.

Las instrucciones de la api están aquí: https://bittrex.com/home/api

Saludos
 

carlos7000

Well-Known Member
Licensed User
Longtime User
Encontré una función parecida a la hash_hmac de php. Es esta:

B4X:
Sub HashHmac(data As String, secret As String) As Byte()
    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
End Sub

La encontré aquí https://www.b4x.com/android/forum/threads/connecting-securely-with-httputils2.38207/#post-226362

Pero sigo atorado.
 

carlos7000

Well-Known Member
Licensed User
Longtime User
Les cuento que he avanzado hasta aquí:

B4X:
Sub ButtonLogin_Click
'    $apikey='xxx';
'    $apisecret='xxx';
'    $nonce=time();
'    $uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
'    $sign=hash_hmac('sha512',$uri,$apisecret);
'    $ch = curl_init($uri);
'    curl_setopt($ch, CURLOPT_HTTPHEADER, Array('apisign:'.$sign));
'    $execResult = curl_exec($ch);
'    $obj = json_decode($execResult);

    Dim API_Key  As String
    Dim API_Secret  As String
    Dim nonce As Int  
    Dim Post_URL As String
    Dim sign() As Byte
    Dim API_Signed As String
    Dim Byte_Conv As ByteConverter
    Dim Post_Data As String
    Dim uri As String
  
    EditTextJson.Text = ""
    EditTextOut.Text = ""
  
    Post_URL = "https://bittrex.com/api/v1.1/market/getopenorders"
    Post_Data = "apikey=" & API_Key  & "&nonce=" & nonce
  
  
    'Read Only
    'Key:00e2ccecef704aa683563f6bb91567e8
    'Secret:224559c2a1c14200938dc9bf5bb69d4b
   
    '    $apikey='xxx';
    API_Key    = "00e2ccecef704aa683563f6bb91567e8"
    '    $apisecret='xxx';
    API_Secret = "224559c2a1c14200938dc9bf5bb69d4b"
    '    $nonce=time();
    nonce = DateTime.Now/1000
    '    $uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
    uri = Post_URL & "?apikey=" & API_Key & "&nonce=" & nonce
    '    $sign=hash_hmac('sha512',$uri,$apisecret);
    sign = HashHmac(uri, API_Secret)
  
    API_Signed = Byte_Conv.HexFromBytes(sign) 'convert to HEX
    API_Signed = API_Signed.ToLowerCase
  
    Dim job_GetInfo As HttpJob

    job_GetInfo.Initialize("job_GetInfo", Me)
    job_GetInfo.PostString(Post_URL, Post_Data)
    job_GetInfo.GetRequest.SetHeader("apisign", API_Signed)
    job_GetInfo.GetRequest.SetHeader("apikey", API_Key)

    'Stuff that appears unnecessary but since others use it.....
    job_GetInfo.GetRequest.SetHeader("User-Agent","Mozilla/4.0 (compatible; Cryptsy API B4A client)")
    job_GetInfo.GetRequest.SetContentType("application/x-www-form-urlencoded")
End Sub

private Sub jobDone (job1 As HttpJob)
    Log(job1.Success)
    Log(job1.GetString)
  
    If (job1.JobName == "job_GetInfo" ) Then
        Try
            EditTextOut.Text = job1.GetString
        Catch
            Log(LastException)
        End Try
    End If
End Sub

He jugado cambiando variando varias cosas, pero nada funciona. La respuesta siempre es

{"success":false,"message":"APIKEY_NOT_PROVIDED","result":null}
 
Top