Link to home
Start Free TrialLog in
Avatar of BeWyched
BeWyched

asked on

Convert cURL to VB.NET

Not sure what happened there - I thought I'd submitted this but its disappeared - so sorry if my question is duplicated!

Anyway, I need to convert the following cURL command to vb.net to obtain an access token from an OAuth2 authorisation server. I'm normally reasonably competent handling HTTP requests in vb.net but I just can't get this to work. can anyone help please?

curl  -u  ID_Key:Secret_Key  https://targetURL.com  -d  'grant_type=client_credentials'

My coding is:

        Dim credstring As String = "ID_Key:Secret_Key"
        Dim authstring As String = Convert.ToBase64String(Encoding.UTF8.GetBytes(credstring))

        Dim webrequest As WebRequest = webrequest.Create("https://targetURL.com?grant_type=client_credentials")

        With webrequest
            .Headers.Add("Authorization", "BASIC " & authstring)

            Dim webresponse As HttpWebResponse = .GetResponse() '

            Dim stream As System.IO.Stream = webresponse.GetResponseStream()
            Dim reader As New StreamReader(stream, Encoding.UTF8)
            Dim contents As String = reader.ReadToEnd()
            Debug.Print(contents)
        End With


Obviously the ID, Key and URL will be the real thing.

Many thanks.
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BeWyched
BeWyched

ASKER

Many thanks gr8gonzo.

I had to tweak your coding but your post pointed me in the right direction - it now works!!
Super-fast response!
BeWyched ,

Would you mind sharing your code?  

I have the same question and would like to see how you did it in VB.net after tweaking the sample.

Thanks