PayPal获取访问令牌vb.net请求已中止:无法创建SSL / TLS安全通道

时间:2018-06-12 07:30:35

标签: vb.net ssl paypal-sandbox

我有几个关于调用PayPal的Grant Access Token API的问题,下面是我的代码:

Public Function testAPI() As String
    Dim data As StringBuilder
    Dim byteData() As Byte
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim postStream As Stream = Nothing
    Dim reader As StreamReader
    Dim Result As String = ""
    request = DirectCast(WebRequest.Create("https://api.sandbox.paypal.com/v1/oauth2/token"), HttpWebRequest)
    request.Headers.Add("Username", "XXXXXXXXXX")
    request.Headers.Add("Password", "XXXXXXXXXX")
    data = New StringBuilder("{""grant_type"":""client_credentials""}")
    request.ContentType = "application/x-www-form-urlencoded"
    request.Method = WebRequestMethods.Http.Post
    byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())
    request.ContentLength = byteData.Length
    postStream = request.GetRequestStream()
    postStream.Write(byteData, 0, byteData.Length)
    If Not postStream Is Nothing Then postStream.Close()
    response = DirectCast(request.GetResponse(), HttpWebResponse)
    reader = New StreamReader(response.GetResponseStream())
    Result = reader.ReadToEnd().ToString
    Return Result
End Function
  1. 我是否以正确的方式传递用户名和密码?
  2. 我通过" grant_type":" client_credentials"以正确的方式?
  3. 我一直收到错误:请求已中止:无法创建SSL / TLS安全通道。我试图添加一些我从谷歌搜索的代码:

    ServicePointManager.Expect100Continue = True
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    request.ProtocolVersion = HttpVersion.Version11
    
  4. 但它仍然失败并返回相同的错误消息。请帮忙。

0 个答案:

没有答案