Results 1 to 8 of 8

Thread: [RESOLVED] Passing credential and apikey in headers.

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Resolved [RESOLVED] Passing credential and apikey in headers.

    Hi,

    I'm trying to load up my request header but am havig zero luck
    the example shows this

    Code:
    When using our APIs and Web Services, you must pass the API Key using the /authenticate method to request a bearer token.
    For all customers, MyEstes credentials (MyEstes username and password) must be passed in the header.
    
    
    Test environment example:
    
    curl -X POST "https://uat-cloudapi.estes-express.com/authenticate" -H "accept: application/json" -H "apikey: 0101010101"
    
    The response JSON will contain the bearer token. (See JSON example on right.)
    I've tried the code below and also some code using a webclient and Postasync functions.
    I keep getting the "UnAuthorized" respons/

    I have to pass in my userid and password in the header and the APIkey as shown in the example.

    Any help will be greatly appreciated.

    Thanks



    Code:
    Dim Username As String = ("MYUSER")
    Dim Password As String = ("MYPWD")
    Dim apikey As String = ("MYAPIKEY")
    
    Dim request As HttpWebRequest = CType(WebRequest.Create("https://uat-cloudapi.estes-express.com/authenticate"), HttpWebRequest)
    request.Method = "POST"
    request.Accept = "application/json"
    request.Headers.Add("Authorization", $"apikey:{apikey}")
    request.Headers.Add("Authorization", $"username:{Username}")
    request.Headers.Add("Authorization", $"passowrd:{Password}")
    
    
    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)

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

    Re: Passing credential and apikey in headers.

    The curl example you posted doesn't pass any credentials, does the example work?

    Also in your code you have "passowrd" not "password" for the header name - that might be the issue if this is a direct copy and paste from your code.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: Passing credential and apikey in headers.

    @PlausiblyDamp

    fixed my misspell thanks. Still won't work. Yeah I noticed the curl example lacked the userid and pwd. I'm gonna contact the API folks and ask why. I'll be back

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: Passing credential and apikey in headers.

    @PlausiblyDamp

    they sent me a corrected curl statement . it's still being weird ,,,,,

    curl -X POST "https://cloudapi.estes-express.com/authenticate" -H "accept: application/json" -H "apikey: [apiKey]" -H "Authorization: Basic [USERNAME]:[PASSWORD]"

  5. #5
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,167

    Re: Passing credential and apikey in headers.

    This sample for Basic Authorization is flawed because [USERNAME]:[PASSWORD] combo has to be base64 encoded.

    With curl you can use -u option to pass user and password and format Basic Authorization header correctly i.e.

    curl -X POST "https://cloudapi.estes-express.com/authenticate" -H "accept: application/json" -H "apikey: [apiKey]" -u "[USERNAME]:[PASSWORD]"

    . . . should work.

    First try this from command line before attempting to implement it in code.

    When you get this working from cmd just paste the curl command in ChatGPT and ask it to make a VB.Net function of it with such and such parameters -- user, pass, api_key, whatever you need as a developer.

    cheers,
    </wqw>

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: Passing credential and apikey in headers.

    Thanks @wqweto . Never used that ChatGPT or trying curl through a command line. (Still havent tried cmd line) I like that ChatGPD. It wrote a nice async function for me which I've used
    Problem is it errors out with

    {
    "error": {
    "details": "Invalid credentials",
    "code": 30020,
    "message": "Failed authentication"
    }
    }


    I've contacted the API folks and asked tem to verify my credentials. Here's the function.

    Code:
       Protected Async Function GetBearertokenAsync() As Task
    
    
           'curl -X POST "https://uat-cloudapi.estes-express.com/authenticate" -H "accept: application/json" -H "apikey: 0101010101"
    
    
    
    
           Dim Username As String = ("my user")
           Dim Password As String = ("my password")
           Dim apikey As String = ("my api key")
           Dim apiUrl As String = "https://uat-cloudapi.estes-express.com/authenticate"
    
    
           Using client As New HttpClient()
    
               ' Add headers
               client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
               client.DefaultRequestHeaders.Add("apikey", apikey)
    
               ' Build Basic Auth header
               Dim authValue = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{Username}:{Password}"))
               client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Basic", authValue)
    
               ' Send POST request
               Dim response As HttpResponseMessage = Await client.PostAsync(apiUrl, Nothing)
    
               If response.IsSuccessStatusCode Then
                   Dim jsonResponse As String = Await response.Content.ReadAsStringAsync()
                   Console.WriteLine("? Authentication successful!")
                   Console.WriteLine("Response JSON:")
                   Console.WriteLine(jsonResponse)
               Else
                   Console.WriteLine($"? Authentication failed: {response.StatusCode}")
                   Dim errorContent As String = Await response.Content.ReadAsStringAsync()
                   Console.WriteLine(errorContent)
               End If
           End Using
    
           Console.WriteLine("Press any key to exit...")
           Console.ReadKey()
    
       End Function

  7. #7

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: Passing credential and apikey in headers.

    @wqweto

    Got my response finally. Seems they had to reset my PWD so it accept my credentials. I'm stoked and most important have ny beaer toekn.
    Thanks for the lead to ChatGPT I'm really liking that place ha ha.

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