Results 1 to 8 of 8

Thread: [RESOLVED] HTTPrequest (POST) Request API key Error

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Resolved [RESOLVED] HTTPrequest (POST) Request API key Error

    I'm trying to obtain an API key via an HTTPrequest (POST).
    I'm using the “TEST server” data they provided.
    Their website API instructions are at :
    https://developer.estes-express.com/...kyNzc.#API-Key

    The example provided by the API folks is:Test example only:

    Code:
    curl -X POST 'https://uat-cloudapi.estes-express.com/v1/api-key'
    -H 'accept: application/json'
    -u ClientID:ClientSecret
    {redacted}'
    Basically it says if you send in the client-id and client-secret in the header the response will return the API key.
    I keep getting an error "401 Not Authorized” .

    My code is below. am I missing something?

    thanks

    -dan

    Code:
          Dim clientid As String = ("{redacted}")
           Dim clientsecret As String = ("{redacted}")
    
    
              Dim response As HttpWebResponse = Nothing
           Dim request As HttpWebRequest = CType(WebRequest.Create("https://uat-cloudapi.estes-express.com/v1/api-key"), HttpWebRequest)
    
           request.Method = ("POST")
           request.Accept = "application/json"
           request.Headers.Add(clientid, clientsecret)
    
           Try
               Dim httpResponse As System.Net.HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
               Dim sr As System.IO.StreamReader = New System.IO.StreamReader(httpResponse.GetResponseStream())
               sr.Close()
    
    
           Catch ex As WebException
               Dim errmsg As String = ex.Message
               If ex.Response IsNot Nothing Then
                   Dim httpResponse As HttpWebResponse = DirectCast(ex.Response, HttpWebResponse)
                   Console.WriteLine($"HTTP Error: {httpResponse.StatusCode} - {httpResponse.StatusDescription}")
    
               End If
    
           End Try
    Last edited by dday9; Oct 30th, 2025 at 08:19 AM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: HTTPrequest (POST) Request API key Error

    Heads up, but I have modified your post to wrap the code in [CODE][/CODE] tags. I also removed your client id and secret as you really don't want to be sharing that information.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: HTTPrequest (POST) Request API key Error

    Regarding your specific issue, the documentation states that you need to send your ClientID and ClientSecret as Username & Password for basic auth.

    Right now, you're just adding them as a simple header instead of an authorization header.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: HTTPrequest (POST) Request API key Error

    @dday9

    Thanks. They were test only client id and secrets to be used on their test server. I wouldn't publish the live ones.
    Appreciate you looking out for me though

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: HTTPrequest (POST) Request API key Error

    Ok I'll add the Auth header when I get back to my dev box. something like this

    request.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("ClientID ", ClientSecret )

    Thank You very much. I'll let you know how it goes.

    -dan

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: HTTPrequest (POST) Request API key Error

    @dday9

    The example I was given looks like this.

    Code:
    curl -X POST 'https://uat-cloudapi.estes-express.com/v1/api-key'
    -H 'accept: application/json'
    -u ClientID:ClientSecret
    ZThkNmM4Y2MtMDY3ZC00ZmY4LTgxYzMtNWJmNjAxNjUzYjYwOjUyZmRmYzA3LTIxODItNjU0Zi0xNjNmLTVmMGY5YTYyMWQ3Mg'
    What I'm seeing when I dump my response is:

    Code:
    ?request.Headers.count
    2
    ?request.Headers(0)
    "application/json"
    ?request.Headers(1)
    "Basic MGYxZTdmNmUtZWMzNy00NWRlLWJjYjAtN2M3MzBkMTkxMTJmOmVjNjgzZWFmLWE0MGUtNDliZC1iMmRlLWMwYmNiNDE0ZjcyOQ=="
    My Code now is as follows:

    Code:
        Dim clientid As String = ("MY CLIENT ID")
        Dim clientsecret As String = ("My CLient Secret")
    
        Dim credentials As String = $"{clientid}:{clientsecret}"
        Dim encodedCredentials As String = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials))
    
    
        Dim request As HttpWebRequest = CType(WebRequest.Create("https://uat-cloudapi.estes-express.com/v1/api-key"), HttpWebRequest)
    
        request.Method = ("POST")
        request.Accept = "application/json"
        request.Headers.Add("Authorization", $"Basic {encodedCredentials}")
    I'm Missing something.

  7. #7
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: HTTPrequest (POST) Request API key Error

    WebRequest has a Credentials property https://learn.microsoft.com/en-us/do...s?view=net-9.0 - that should ensure the correct headers etc. are set.

    https://learn.microsoft.com/en-us/do...l?view=net-9.0 gives an example of how to create a Credentials object and use it.

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: HTTPrequest (POST) Request API key Error

    This Worked.



    Code:
    
            Dim clientid As String = ("MY Client ID")
            Dim clientsecret As String = ("My Secret")
            Dim credentials As String = $"{clientid}:{clientsecret}"
            Dim encodedCredentials As String = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials))
    
            Dim request As HttpWebRequest = CType(WebRequest.Create("https://uat-cloudapi.estes-express.com/v1/api-key"), HttpWebRequest)
            request.Method = "POST"
            request.Accept = "application/json"
            request.Headers.Add("Authorization", $"Basic {encodedCredentials}")
    
           Try
    
                Dim httpResponse As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
                Dim Returneddata As New StreamReader(httpResponse.GetResponseStream())
                Dim RTNJSON As String = Returneddata.ReadToEnd()
    
                Dim JSONresponse = JsonConvert.DeserializeObject(Of HttpResponse)(RTNJSON)
    
            Catch ex As WebException
                Dim errmsg As String = ex.Message
                If ex.Response IsNot Nothing Then
                    Dim httpResponse As HttpWebResponse = DirectCast(ex.Response, HttpWebResponse)
                    Console.WriteLine($"HTTP Error: {httpResponse.StatusCode} - {httpResponse.StatusDescription}")
    
                End If
            End Try

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width