0
votes

Hi everyone i'm new here. I'm trying to connect to a website but it's giving me a 403 error.

I'm hoping somebody could help me, here's what the website says:

Message is formed by concatenating these four strings:

  • Nonce. A 63 bit positive integer.

  • HMAC authentication key. This is the first one of a key/secret pair.

  • Relative path, for example /api/wallet/

  • GET or POST parameters in their URL encoded format.

Hashing algorithm is SHA256.

Sending signature

Signature is sent via HTTP headers. A total of three fields are needed:

  • Apiauth-Key: HMAC authentication key.

  • Apiauth-Nonce: The nonce in this particular request.

  • Apiauth-Signature: HMAC signature.

Here's my code, i'd be ever so grateful if anybody could help me open connection to the site, thanks

Imports System
Imports System.Web.Services
Imports System.Net
Imports System.IO
Imports System.Security.Cryptography
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.Text
Imports System.Web

Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Call authtest()
    End Sub

    Sub authtest()
        Dim webStream As Stream
        Dim webResponse = ""
        Dim req As WebRequest
        Dim res As HttpWebResponse

        Dim hmacauthkey As String = "<myapikey>"
        Dim hmacsecret As String = "<myapisecret>"
        Dim r As New Random
        Dim nonce1 As Long = Math.Round(r.NextDouble * Long.MaxValue)
        Dim relativepath As String = "/api/myself/"
        Dim params As String = ""
        Dim getparamsurlencoded = WebUtility.HtmlEncode(params)
        Dim message As String = Convert.ToString(nonce1) & hmacauthkey & relativepath & getparamsurlencoded
        Dim signature As String = HashString(message)

        req = CType(WebRequest.Create("https://website.com/api/myself/"), HttpWebRequest)
        req.Credentials = New NetworkCredential("<username>", "<password>")
        req.ContentType = "application/x-www-form-urlencoded"

        req.Headers.Add("Apiauth-Key", "<myapikey>")
        req.Headers.Add("Apiauth-Nonce", Convert.ToString(nonce1))
        req.Headers.Add("Apiauth-Signature", signature)
        req.Method = "GET"
        res = CType(req.GetResponse(), HttpWebResponse) ' Send Request
        webStream = res.GetResponseStream() ' Get Response
        Dim webStreamReader As New StreamReader(webStream)
        While webStreamReader.Peek >= 0
            webResponse = webStreamReader.ReadToEnd()
        End While
        MsgBox(webResponse)
    End Sub

    Public Shared Function HashString(ByVal StringToHash As String) As String
        Dim myEncoder As New System.Text.UTF8Encoding
        Dim Key() As Byte = myEncoder.GetBytes("<mysecretkey>")
        Dim Text() As Byte = myEncoder.GetBytes(StringToHash)
        Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
        Dim HashCode As Byte() = myHMACSHA256.ComputeHash(Text)
        Return StrConv(Convert.ToBase64String(HashCode), vbUpperCase)
    End Function

    Function HMACSHA256_Encrypt(ByVal Txt As String) As String
        Try
            Dim secretkey As String = "<mysecretkey>"
            Dim sha As New System.Security.Cryptography.HMACSHA256(System.Text.UTF8Encoding.UTF8.GetBytes(secretkey))
            Dim Hash() As Byte = sha.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(Txt))
            Dim sb As New System.Text.StringBuilder(Hash.Length * 2)
            For Each B As Byte In Hash
                sb.Append(Hex(B).PadLeft(2, "0"))
            Next
            Return sb.ToString.ToLower
        Catch ex As Exception
            Debug.Print(Date.Now & " SHA256_Encrypt error " & ex.Message)
            Return Nothing
        End Try
    End Function
End Class
1

1 Answers

0
votes

Is there a reason you're passing network credentials in your web request?

I'd recommend running Fiddler (or something similar) while making your web request and check to see if you can view more details surrounding the response.