【问题标题】:How to get an OAuth 2.0 access token in VB.NET如何在 VB.NET 中获取 OAuth 2.0 访问令牌
【发布时间】:2019-09-10 16:49:21
【问题描述】:

如何通过在 VB.NET 中交换基本凭据(客户端 ID 和密码)(OAuth 身份验证)来请求令牌?

我尝试了下面的代码,但没有成功。我收到错误消息:

底层连接已关闭:发送时发生意外错误。

代码

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

【问题讨论】:

  • 如果您在运行代码时能够更加具体和准确地了解问题所在,这将很有帮助
  • 我收到错误消息:底层连接已关闭:发送时发生意外错误。
  • 使用 WebRequest,一个 Https 连接,您需要明确设置 TLS1.2 协议。添加ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12。握手可能还需要ServicePointManager.ServerCertificateValidationCallback。添加一个只返回 True 的(除非您确实想要/需要验证服务器证书)。
  • 请将完整的错误消息和堆栈跟踪添加到您的问题中,谢谢
  • 现在它说:远程服务器返回错误:(500)

标签: vb.net


【解决方案1】:

最好的方法是使用 RestClient。您可以通过 NuGet 包管理器将其添加到您的项目中。

以下是如何请求令牌:

 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

【讨论】:

  • ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications... 您知道这样做实际上是在禁用 SSL 保护以防止中间人攻击?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 2011-02-28
  • 2023-01-23
  • 1970-01-01
  • 2017-06-23
  • 2016-10-05
相关资源
最近更新 更多