Android Question Bittrex Api authentication [SOLUCIONADO]

carlos7000

Well-Known Member
Licensed User
Longtime User
Hello

I'm trying to create an application several days ago to see my balance in bittrex (https://bittrex.com), see my open orders, etc.

The code I am using is a modified version of the code that I find here Connecting securely with HttpUtils2?

The code I'm testing is this:

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
            Log(job1.GetString)
        Catch
            Log(LastException)
        End Try
    End If
End Sub

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

I have played by changing several things, but nothing works. I always get the same answer:

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

Notes:
  • The Key and the Secret are real, but safe (only for tests, only allow reading, no trading, no margin trading, no withdrawal).
  • Information about the bittrex api can be found here https://bittrex.com/home/api
  • Required libraries: Encryption, ByteConverter, OkHttp and OkHttpUtils2

¿What could be the problem?
 
Last edited:

monic

Active Member
Licensed User
Longtime User
Based on Erel response does this work, I'm learning too ;) I change the api to point to market rate to show some usefull data.

B4X:
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 = ""
    Post_URL = "https://bittrex.com/api/v1.1/public/getmarkets"
    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.Download2(Post_URL, _
      Array As String("apikey", API_Key, "nonce", nonce))
    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

I get a success 'True' response
 

Attachments

  • bittrex.zip
    3.8 KB · Views: 404
Last edited:
Upvote 0

carlos7000

Well-Known Member
Licensed User
Longtime User
Based on Erel response does this work, I'm learning too ;) I change the api to point to market rate to show some usefull data.

B4X:
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 = ""
    Post_URL = "https://bittrex.com/api/v1.1/public/getmarkets"
    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.Download2(Post_URL, _
      Array As String("apikey", API_Key, "nonce", nonce))
    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

I get a success 'True' response

It works very well.

Thank you
 
Upvote 0
Top