Android Question Autenticacion Https API [solved]

carlos7000

Active Member
Licensed User
Hi.
I am trying to create a simple application that allows me to interact with Poloniex Https server. To do this I must use your Api. Try to run the returnBalances command, to know my balances. The code I used was this:

B4X:
Private Sub returnBalances
    Dim command As String
    command = "https://poloniex.com/tradingApi/command=returnBalances"
    SendCommand("returnBalances", command, True)
End Sub

private Sub SendCommand(work As String, uri As String, Signature As Boolean)
    Try
        If (Signature == True)Then
            Dim nonce As Long
            nonce = DateTime.now
            uri = uri & "&nonce=" & nonce
 
            Dim Sign() As Byte
            Sign = Https.HashHmac( uri, X_ApiSecret)  
     
            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)
        j.PostString(uri, "")
 
        If (Signature == True)Then
            j.GetRequest.SetHeader("Key", X_ApiKey)
            j.GetRequest.SetHeader("Sign", API_Signed)
        End If
    Catch
        ToastMessageShow("SendCommand" & LastException, True)
    End Try
End Sub

private Sub jobDone (job As HttpJob)
    Dim Recibido As String
 
    Main.error = job.ErrorMessage
    CallSub2( Main, "ShowError", job.ErrorMessage)
 
    If(job.Success) Then 
        'GetBalance
        If(job.JobName == "returnBalances") Then
            Dim parser As JSONParser
            Recibido = job.GetString
            CuentaData(Recibido)
            parser.Initialize(Recibido)
            If(Recibido.Contains("true") == True) Then
                Dim root As Map = parser.NextObject
                Dim result As Map = root.Get("result")
 
                X_Available = Str2Double(result.Get("Available"))
                X_Balance = Str2Double(result.Get("Balance"))
     
            End If
        End If
    Else
        Log(job.ErrorMessage & "/" & LastException)
    End If
 
    job.Release
End Sub
When executing the command I receive a 404 error and an HTML page informing that the requested page was not found.

What could be the mistake?

If you want to test the code, you can use these keys:

ApiKey: KE5G97MT-TH31A6TJ-IXQUK3NJ-69Q76GJG
Secret: 5bdbcb41815a52a7e4427fe165ebb7718ab712ef69224b3ff42d0d0f7db8cae33c5100b14a44b4d74e26797892ea7df6d8bfaf1d947362027c726f5e6ea00bdf
Note:
  • The keys are for testing purposes only and do not allow you to buy or sell cryptocurrencies or transfer funds.
  • My account's Api access, is active
  • Documentation about the api of poloniex here: https://docs.poloniex.com/#returnticker

Thanks.
 
Last edited:

DonManfred

Expert
Licensed User
# Note: set the nonce to the current milliseconds. For example: date +%s00000
echo -n "command=returnCompleteBalances&nonce=154264078495300" | \
openssl sha512 -hmac $API_SECRET

curl -X POST \
-d "command=returnCompleteBalances&nonce=154264078495300" \
-H "Key: 7BCLAZQZ-HKLK9K6U-3MP1RNV9-2LS1L33J" \
-H "Sign: 2a7849ecf...ae71161c8e9a364e21d9de9" \
https://poloniex.com/tradingApi
Based on this the url should be something like

command=returnCompleteBalances&nonce=154264078495300 must be part of the payload.
 

DonManfred

Expert
Licensed User
Here a working example.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    returnBalances
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
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
    Catch
        Log(LastException)
    End Try
    'sign the loaded data using the secret key, return the calc signature data
End Sub
Private Sub returnBalances
    Dim url As String = "https://poloniex.com/tradingApi"
    Dim nonce As Long
    nonce = DateTime.now
    Dim pars As Map = CreateMap("nonce":nonce,"command":"returnBalances")

    SendCommand("returnBalances", url, pars,True)
End Sub

private Sub SendCommand(work As String, uri As String, pars As Map, Signature As Boolean)
    If Signature Then

        Dim tosign As String = $"command=${pars.Get("command")}&nonce=${pars.Get("nonce")}"$
      
        Dim Sign() As Byte
        Sign = HashHmac( tosign, "5bdbcb41815a52a7e4427fe165ebb7718ab712ef69224b3ff42d0d0f7db8cae33c5100b14a44b4d74e26797892ea7df6d8bfaf1d947362027c726f5e6ea00bdf")
  
        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)
    j.PostBytes(uri,tosign.GetBytes("UTF8"))
    If Signature Then
        j.GetRequest.SetHeader("Key", "KE5G97MT-TH31A6TJ-IXQUK3NJ-69Q76GJG")
        j.GetRequest.SetHeader("Sign", API_Signed)
    End If
    Wait For (j) jobDone(job As HttpJob)
    If j.Success Then
        Dim resultfromapi As String = job.GetString
        Dim parser As JSONParser
        parser.Initialize(resultfromapi)
        Dim root As Map = parser.NextObject
        For Each key As String In root.Keys
            Log($"Key "${key}" = ${root.Get(key)}"$)
        Next
    Else
        Log(job.ErrorMessage)
    End If
    job.Release 
End Sub
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Key "1CR" = 0.00000000
Key "ABY" = 0.00000000
Key "AC" = 0.00000000
Key "ACH" = 0.00000000
Key "ADN" = 0.00000000
Key "AEON" = 0.00000000
Key "AERO" = 0.00000000
Key "AIR" = 0.00000000
Key "AMP" = 0.00000000
Key "APH" = 0.00000000
Key "ARCH" = 0.00000000
Key "ARDR" = 0.00000000
Key "ATOM" = 0.00000000
Key "AUR" = 0.00000000
Key "AXIS" = 0.00000000
Key "BALLS" = 0.00000000
Key "BANK" = 0.00000000
Key "BAT" = 0.00000000
Key "BBL" = 0.00000000
Key "BBR" = 0.00000000
Key "BCC" = 0.00000000
Key "BCH" = 0.00000000
Key "BCHABC" = 0.00000000
Key "BCHSV" = 0.00000000
Key "BCN" = 0.00000000
Key "BCY" = 0.00000000
Key "BDC" = 0.00000000
Key "BDG" = 0.00000000
Key "BELA" = 0.00000000
Key "BITCNY" = 0.00000000
Key "BITS" = 0.00000000
Key "BITUSD" = 0.00000000
Key "BLK" = 0.00000000
Key "BLOCK" = 0.00000000
Key "BLU" = 0.00000000
Key "BNS" = 0.00000000
Key "BNT" = 0.00000000
Key "BONES" = 0.00000000
Key "BOST" = 0.00000000
Key "BTC" = 0.00000000
Key "BTCD" = 0.00000000
Key "BTCS" = 0.00000000
Key "BTM" = 0.00000000
Key "BTS" = 0.00000000
Key "BURN" = 0.00000000
Key "BURST" = 0.00000000
Key "C2" = 0.00000000
Key "CACH" = 0.00000000
Key "CAI" = 0.00000000
Key "CC" = 0.00000000
Key "CCN" = 0.00000000
Key "CGA" = 0.00000000
Key "CHA" = 0.00000000
Key "CINNI" = 0.00000000
Key "CLAM" = 0.00000000
Key "CNL" = 0.00000000
Key "CNMT" = 0.00000000
Key "CNOTE" = 0.00000000
Key "COMM" = 0.00000000
Key "CON" = 0.00000000
Key "CORG" = 0.00000000
Key "CRYPT" = 0.00000000
Key "CURE" = 0.00000000
Key "CVC" = 0.00000000
Key "CYC" = 0.00000000
Key "DAO" = 0.00000000
Key "DASH" = 0.00000000
Key "DCR" = 0.00000000
Key "DGB" = 0.00000000
Key "DICE" = 0.00000000
Key "DIEM" = 0.00000000
Key "DIME" = 0.00000000
Key "DIS" = 0.00000000
Key "DNS" = 0.00000000
Key "DOGE" = 0.00000000
Key "DRKC" = 0.00000000
Key "DRM" = 0.00000000
Key "DSH" = 0.00000000
Key "DVK" = 0.00000000
Key "EAC" = 0.00000000
Key "EBT" = 0.00000000
Key "ECC" = 0.00000000
Key "EFL" = 0.00000000
Key "EMC2" = 0.00000000
Key "EMO" = 0.00000000
Key "ENC" = 0.00000000
Key "EOS" = 0.00000000
Key "ETC" = 0.00000000
Key "ETH" = 0.00000000
Key "eTOK" = 0.00000000
Key "EXE" = 0.00000000
Key "EXP" = 0.00000000
Key "FAC" = 0.00000000
Key "FCN" = 0.00000000
Key "FCT" = 0.00000000
Key "FIBRE" = 0.00000000
Key "FLAP" = 0.00000000
Key "FLDC" = 0.00000000
Key "FLO" = 0.00000000
Key "FLT" = 0.00000000
Key "FOAM" = 0.00000000
Key "FOX" = 0.00000000
Key "FRAC" = 0.00000000
Key "FRK" = 0.00000000
Key "FRQ" = 0.00000000
Key "FVZ" = 0.00000000
Key "FZ" = 0.00000000
Key "FZN" = 0.00000000
Key "GAME" = 0.00000000
Key "GAP" = 0.00000000
Key "GAS" = 0.00000000
Key "GDN" = 0.00000000
Key "GEMZ" = 0.00000000
Key "GEO" = 0.00000000
Key "GIAR" = 0.00000000
Key "GLB" = 0.00000000
Key "GML" = 0.00000000
Key "GNO" = 0.00000000
Key "GNS" = 0.00000000
Key "GNT" = 0.00000000
Key "GOLD" = 0.00000000
Key "GPC" = 0.00000000
Key "GPUC" = 0.00000000
Key "GRC" = 0.00000000
Key "GRCX" = 0.00000000
Key "GRIN" = 0.00000000
Key "GRS" = 0.00000000
Key "GUE" = 0.00000000
Key "H2O" = 0.00000000
Key "HIRO" = 0.00000000
Key "HOT" = 0.00000000
Key "HUC" = 0.00000000
Key "HUGE" = 0.00000000
Key "HVC" = 0.00000000
Key "HYP" = 0.00000000
Key "HZ" = 0.00000000
Key "IFC" = 0.00000000
Key "INDEX" = 0.00000000
Key "IOC" = 0.00000000
Key "ITC" = 0.00000000
Key "IXC" = 0.00000000
Key "JLH" = 0.00000000
Key "JPC" = 0.00000000
Key "JUG" = 0.00000000
Key "KDC" = 0.00000000
Key "KEY" = 0.00000000
Key "KNC" = 0.00000000
Key "LBC" = 0.00000000
Key "LC" = 0.00000000
Key "LCL" = 0.00000000
Key "LEAF" = 0.00000000
Key "LGC" = 0.00000000
Key "LOL" = 0.00000000
Key "LOOM" = 0.00000000
Key "LOVE" = 0.00000000
Key "LPT" = 0.00000000
Key "LQD" = 0.00000000
Key "LSK" = 0.00000000
Key "LTBC" = 0.00000000
Key "LTC" = 0.00000000
Key "LTCX" = 0.00000000
Key "MAID" = 0.00000000
Key "MANA" = 0.00000000
Key "MAST" = 0.00000000
Key "MAX" = 0.00000000
Key "MCN" = 0.00000000
Key "MEC" = 0.00000000
Key "METH" = 0.00000000
Key "MIL" = 0.00000000
Key "MIN" = 0.00000000
Key "MINT" = 0.00000000
Key "MMC" = 0.00000000
Key "MMNXT" = 0.00000000
Key "MMXIV" = 0.00000000
Key "MNTA" = 0.00000000
Key "MON" = 0.00000000
Key "MRC" = 0.00000000
Key "MRS" = 0.00000000
Key "MTS" = 0.00000000
Key "MUN" = 0.00000000
Key "MYR" = 0.00000000
Key "MZC" = 0.00000000
Key "N5X" = 0.00000000
Key "NAS" = 0.00000000
Key "NAUT" = 0.00000000
Key "NAV" = 0.00000000
Key "NBT" = 0.00000000
Key "NEOS" = 0.00000000
Key "NL" = 0.00000000
Key "NMC" = 0.00000000
Key "NMR" = 0.00000000
Key "NOBL" = 0.00000000
Key "NOTE" = 0.00000000
Key "NOXT" = 0.00000000
Key "NRS" = 0.00000000
Key "NSR" = 0.00000000
Key "NTX" = 0.00000000
Key "NXC" = 0.00000000
Key "NXT" = 0.00000000
Key "NXTI" = 0.00000000
Key "OMG" = 0.00000000
Key "OMNI" = 0.00000000
Key "OPAL" = 0.00000000
Key "PAND" = 0.00000000
Key "PASC" = 0.00000000
Key "PAWN" = 0.00000000
Key "PIGGY" = 0.00000000
Key "PINK" = 0.00000000
Key "PLX" = 0.00000000
Key "PMC" = 0.00000000
Key "POLY" = 0.00000000
Key "POT" = 0.00000000
Key "PPC" = 0.00000000
Key "PRC" = 0.00000000
Key "PRT" = 0.00000000
Key "PTS" = 0.00000000
Key "Q2C" = 0.00000000
Key "QBK" = 0.00000000
Key "QCN" = 0.00000000
Key "QORA" = 0.00000000
Key "QTL" = 0.00000000
Key "QTUM" = 0.00000000
Key "RADS" = 0.00000000
Key "RBY" = 0.00000000
Key "RDD" = 0.00000000
Key "REP" = 0.00000000
Key "RIC" = 0.00000000
Key "RZR" = 0.00000000
Key "SBD" = 0.00000000
Key "SC" = 0.00000000
Key "SDC" = 0.00000000
Key "SHIBE" = 0.00000000
Key "SHOPX" = 0.00000000
Key "SILK" = 0.00000000
Key "SJCX" = 0.00000000
Key "SLR" = 0.00000000
Key "SMC" = 0.00000000
Key "SNT" = 0.00000000
Key "SOC" = 0.00000000
Key "SPA" = 0.00000000
Key "SQL" = 0.00000000
Key "SRCC" = 0.00000000
Key "SRG" = 0.00000000
Key "SSD" = 0.00000000
Key "STEEM" = 0.00000000
Key "STORJ" = 0.00000000
Key "STR" = 0.00000000
Key "STRAT" = 0.00000000
Key "SUM" = 0.00000000
Key "SUN" = 0.00000000
Key "SWARM" = 0.00000000
Key "SXC" = 0.00000000
Key "SYNC" = 0.00000000
Key "SYS" = 0.00000000
Key "TAC" = 0.00000000
Key "TOR" = 0.00000000
Key "TRUST" = 0.00000000
Key "TRX" = 0.00000000
Key "TWE" = 0.00000000
Key "UIS" = 0.00000000
Key "ULTC" = 0.00000000
Key "UNITY" = 0.00000000
Key "URO" = 0.00000000
Key "USDC" = 0.00000000
Key "USDE" = 0.00000000
Key "USDT" = 0.00000000
Key "UTC" = 0.00000000
Key "UTIL" = 0.00000000
Key "UVC" = 0.00000000
Key "VIA" = 0.00000000
Key "VOOT" = 0.00000000
Key "VOX" = 0.00000000
Key "VRC" = 0.00000000
Key "VTC" = 0.00000000
Key "WC" = 0.00000000
Key "WDC" = 0.00000000
Key "WIKI" = 0.00000000
Key "WOLF" = 0.00000000
Key "X13" = 0.00000000
Key "XAI" = 0.00000000
Key "XAP" = 0.00000000
Key "XBC" = 0.00000000
Key "XC" = 0.00000000
Key "XCH" = 0.00000000
Key "XCN" = 0.00000000
Key "XCP" = 0.00000000
Key "XCR" = 0.00000000
Key "XDN" = 0.00000000
Key "XDP" = 0.00000000
Key "XEM" = 0.00000000
Key "XHC" = 0.00000000
Key "XLB" = 0.00000000
Key "XMG" = 0.00000000
Key "XMR" = 0.00000000
Key "XPB" = 0.00000000
Key "XPM" = 0.00000000
Key "XRP" = 0.00000000
Key "XSI" = 0.00000000
Key "XST" = 0.00000000
Key "XSV" = 0.00000000
Key "XUSD" = 0.00000000
Key "XVC" = 0.00000000
Key "XXC" = 0.00000000
Key "YACC" = 0.00000000
Key "YANG" = 0.00000000
Key "YC" = 0.00000000
Key "YIN" = 0.00000000
Key "ZEC" = 0.00000000
Key "ZRX" = 0.00000000
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
 

Attachments

carlos7000

Active Member
Licensed User
Here a working example.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    returnBalances
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
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
    Catch
        Log(LastException)
    End Try
    'sign the loaded data using the secret key, return the calc signature data
End Sub
Private Sub returnBalances
    Dim url As String = "https://poloniex.com/tradingApi"
    Dim nonce As Long
    nonce = DateTime.now
    Dim pars As Map = CreateMap("nonce":nonce,"command":"returnBalances")

    SendCommand("returnBalances", url, pars,True)
End Sub

private Sub SendCommand(work As String, uri As String, pars As Map, Signature As Boolean)
    If Signature Then

        Dim tosign As String = $"command=${pars.Get("command")}&nonce=${pars.Get("nonce")}"$
     
        Dim Sign() As Byte
        Sign = HashHmac( tosign, "5bdbcb41815a52a7e4427fe165ebb7718ab712ef69224b3ff42d0d0f7db8cae33c5100b14a44b4d74e26797892ea7df6d8bfaf1d947362027c726f5e6ea00bdf")
 
        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)
    j.PostBytes(uri,tosign.GetBytes("UTF8"))
    If Signature Then
        j.GetRequest.SetHeader("Key", "KE5G97MT-TH31A6TJ-IXQUK3NJ-69Q76GJG")
        j.GetRequest.SetHeader("Sign", API_Signed)
    End If
    Wait For (j) jobDone(job As HttpJob)
    If j.Success Then
        Dim resultfromapi As String = job.GetString
        Dim parser As JSONParser
        parser.Initialize(resultfromapi)
        Dim root As Map = parser.NextObject
        For Each key As String In root.Keys
            Log($"Key "${key}" = ${root.Get(key)}"$)
        Next
    Else
        Log(job.ErrorMessage)
    End If
    job.Release
End Sub
Hello, DonManfred

It worked very well.

Thank you.
 
Top