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.
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
The best way is to use RestClient. You can add it to your project from NuGet Package Manager. I've been looking for this answer for a week now. Hopefully it'll help someone in the future. here how to request token:
Function request_token()
ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim client = New RestClient("https://www.ia.provider.com/access_token")
Dim request = New RestRequest(Method.POST)
request.Parameters.Clear()
request.AddParameter("Authorization", "Basic " & System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("545874a9f5134ed0b54545:333650277fa4d50934c65AAb46Ad123")))
request.AddParameter("Accept", "application/json")
request.AddParameter("Content-Type", "application/x-www-form-urlencoded")
request.AddParameter("Content-Length", "29")
request.AddParameter("grant_type", "client_credentials")
Return response.Content.ToString
End Function
Just a minor comment. Mark did not define the response object If someone should be interested in using the example provided by Mark, they should add: Dim response As IRestResponse = request.Execute(request)
as a new line above: Return response.Content.ToString
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.