Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Localbitcoins API HMAC failure.

Sooo i have been fucking with this code for hours now and it is still giving me the error : Response String: The remote server returned an error: (400) Bad Request.

I dont understand why lbc is telling me the request i submitted was bad / invalid.

Code:

Form load:
Code
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Uri As String
        Dim path As String
        Dim statuscode As HttpStatusCode
        Dim dataStream As Stream = Nothing
        Dim StreamReader As StreamReader
        Dim ResponseFromServer As String
        Dim ApiKey As String
        Dim SecretKey As String
        Dim uri_path As String
        Dim total_key As String
        Dim time_in_ms As DateTime = DateTime.Now


        Uri = "https://localbitcoins.com"
        path = "/api/myself/"
        ApiKey = "publickey"
        SecretKey = "secretkey"
        uri_path = Uri & path
        total_key = time_in_ms.Millisecond.ToString & ApiKey & path
        Try
            Dim request As WebRequest = WebRequest.Create(Uri_path)
            request.Timeout = 120000
            request.Method = "POST"

            Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(total_key)

            request.Headers.Add("Apiauth-Key", ApiKey)
            request.Headers.Add("Apiauth-Nonce", time_in_ms.Millisecond.ToString)
            request.Headers.Add("Apiauth-Signature", HashHMAC(SecretKey, total_key).ToUpper)
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = byteArray.Length
            dataStream = request.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()

            Dim WebResponse As WebResponse = request.GetResponse()
            dataStream = WebResponse.GetResponseStream()
            StreamReader = New StreamReader(dataStream)
            ResponseFromServer = StreamReader.ReadToEnd()
            dataStream.Close()
            WebResponse.Close()
            statuscode = HttpStatusCode.OK

        Catch ex As WebException
            If ex.Response IsNot Nothing Then
                dataStream = ex.Response.GetResponseStream()
                Dim reader As New StreamReader(dataStream)
                statuscode = DirectCast(ex.Response, HttpWebResponse).StatusCode
                ResponseFromServer = ex.Message
            Else
                Dim resp As String = ""
                statuscode = HttpStatusCode.ExpectationFailed
                ResponseFromServer = "Error"
            End If

        Catch ex As Exception
            statuscode = HttpStatusCode.ExpectationFailed
            ResponseFromServer = "Error"
        End Try
        'Console.WriteLine("Response Code: " & statuscode)
        TextBox1.AppendText("Response String: " & ResponseFromServer)
        'Console.WriteLine("Response String: " & ResponseFromServer)
    End Sub


HMAC FUNCTION:
Code
Public Function HashHMAC(key As String, message As String) As String
        Dim encoding As System.Text.UTF8Encoding
        Dim keyByte As Byte()
        Dim hmacsha256 As HMACSHA256
        Dim messageBytes As Byte()
        Dim hashmessage As Byte()
        encoding = New System.Text.UTF8Encoding()
        keyByte = encoding.GetBytes(key)
        hmacsha256 = New HMACSHA256(keyByte)
        messageBytes = encoding.GetBytes(message)
        hashmessage = hmacsha256.ComputeHash(messageBytes)
        Return ByteArrayToString(hashmessage)
    End Function
    Public Function ByteArrayToString(ba As Byte()) As String
        Dim hex As StringBuilder
        Dim b As Byte
        hex = New StringBuilder(ba.Length * 2)
        For Each b In ba
            hex.AppendFormat("{0:x2}", b)
        Next
        Return hex.ToString()
    End Function
Well for one, you should use a GET to /api/myself instead of POST

Quote:/api/myself/
Required arguments: None

Access token scope: Read

HTTP Method: GET

Return the information of the currently logged in user (the owner of authentication token). The format is the same as in account_info method.

Taken from https://localbitcoins.com/api-docs/#api_toc1

Hope this helps.
I created a GET request method and it still returns a bad request.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by ThinWhiteDukeOct 10, 2021 03:23 PM 2 107 Oct 10, 2021 03:23 PM
Last Post: ThinWhiteDuke
[Java] by GlushkoJun 13, 2018 11:13 PM 3 642 Jun 13, 2018 11:13 PM
Last Post: hostov
[Python] by TheDeceptionistDec 30, 2017 12:14 AM 0 146 Dec 30, 2017 12:14 AM
Last Post: TheDeceptionist