How can I request a token by exchanging basic credentials (client id and secret) (OAuth authentication) in VB.NET?
I tried the code below without any success. I get the error saying:
The underlying connection was closed: An unexpected error occurred on a send.
Code
Function test()
Try
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://www.ia.provider.com/access_token"), HttpWebRequest)
myHttpWebRequest.Method = "POST"
myHttpWebRequest.ContentType = "application/json"
myHttpWebRequest.Headers.Add("Authorization", "Basic " & System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("545874a9f5134ed0b54545:333650277fa4d50934c65AAb46Ad123")))
Dim myHttpWebResponse As HttpWebResponse CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Dim myreader As New System.IO.StreamReader(myHttpWebResponse.GetResponseStream)
Dim myText As String
myText = myreader.ReadToEnd
Console.WriteLine(myText)
Catch e As ArgumentException
Console.WriteLine(e.Message)
MsgBox((vbLf & "WebException is thrown. " & vbLf & "Message is:" & e.Message))
Catch e As WebException
Console.WriteLine(vbLf & "WebException is thrown. " & vbLf & "Message is :" & e.Message)
If e.Status = WebExceptionStatus.ProtocolError Then
Console.WriteLine("Status Code: {0}", CType(e.Response, HttpWebResponse).StatusCode)
Console.WriteLine("Status Description: {0}", CType(e.Response, HttpWebResponse).StatusDescription)
Console.WriteLine("Server : {0}", CType(e.Response, HttpWebResponse).Server)
End If
Catch e As Exception
Console.WriteLine("Exception is thrown. Message is:" & e.Message)
End Try
End Function