LinkedIn oAuth2 基础连接已关闭:发送时发生意外错误

LinkedIn oAuth2 The underlying connection was closed: An unexpected error occurred on a send

我在连接到 LinkedIn oAuth2 时突然开始收到以下错误 API... 基础连接已关闭:发送时发生意外错误。

这通常是由于 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 未设置引起的。

错误的代码行是

Dim oStream As Stream = oWebRequest.GetRequestStream()

我已经在代码中设置了这个;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

但继续收到错误。如果我 运行 代码在本地运行正常,但在部署的服务器上运行不正常。服务器是 Windows 2012 R2。我在服务器上禁用了 TLS 1.2 之前的所有版本的 SSL/TLS,并重新启动了服务器,但即使在代码中设置了 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12,错误仍然存​​在。

代码 运行 在本地,但我仍然在服务器上收到错误,我没有想法!

Dim sSocialMediaClientID = String.Empty
Dim sSocialMediaSecretKey = String.Empty
Dim sAuthServiceUrl As String = String.Empty
Dim sProfileAccessServiceUrl As String = String.Empty
Dim sEmailAddressAccessServiceUrl As String = String.Empty
sCallBackURL = String.Concat(sCallBackURL, "/RequestHandler/LinkedInCallBack")
sSocialMediaClientID = "Client ID"
sSocialMediaSecretKey = "Client Secret"

sAuthServiceUrl = "https://www.linkedin.com/oauth/v2/accessToken"
sProfileAccessServiceUrl = "https://api.linkedin.com/v2/me?oauth2_access_token={0}"
sEmailAddressAccessServiceUrl = "https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))&oauth2_access_token={0}"
Dim sCode As String = Request.QueryString("code")
sAuthToken = pf_ProcessAuthRequest(sAuthServiceUrl, sCode, sCallBackURL, sSocialMediaClientID, sSocialMediaSecretKey)

   Private Function pf_ProcessAuthRequest(ByVal inAuthServiceUrl As String, ByVal inAuthCode As String, ByVal inCallBackUrl As String, ByVal inClientID As String, ByVal inClientSecret As String) As String
            Dim sAuthToken As String = String.Empty
            Dim oPostData As New StringBuilder()

            oPostData.AppendFormat("grant_type=authorization_code")
            oPostData.AppendFormat("&code={0}", inAuthCode)
            oPostData.AppendFormat("&redirect_uri={0}", HttpUtility.UrlEncode(inCallBackUrl))
            oPostData.AppendFormat("&client_id={0}", inClientID)
            oPostData.AppendFormat("&client_secret={0}", inClientSecret)

            Dim oByteArray As Byte() = Encoding.UTF8.GetBytes(oPostData.ToString())
            '
            ' Create the web request
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12    
            Dim oWebRequest As WebRequest = WebRequest.Create(inAuthServiceUrl)
            oWebRequest.Method = "POST"
            oWebRequest.ContentType = "application/x-www-form-urlencoded"
            oWebRequest.ContentLength = oByteArray.Length
            '
            ' Add the post data
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
            'the next line errors!
            Dim oStream As Stream = oWebRequest.GetRequestStream()
            oStream.Write(oByteArray, 0, oByteArray.Length)
            oStream.Close()
            '
            ' Get the response
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
            Dim oResponse As WebResponse = oWebRequest.GetResponse()
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
            Dim sResultStatus As String = CType(oResponse, HttpWebResponse).StatusDescription

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
            oStream = oResponse.GetResponseStream()

            Dim oReader As StreamReader = New StreamReader(oStream)
            Dim sJSON As String = oReader.ReadToEnd()
            Dim oJObj As JObject = JObject.Parse(sJSON)

            sAuthToken = oJObj("access_token").ToString

            oReader.Close()
            oStream.Close()
            oResponse.Close()

            Return sAuthToken
        End Function

尝试更改 sAuthServiceUrl = "https://www.linkedin.com/oauth/v2/accessToken" 到 sAuthServiceUrl = "https://api.linkedin.com/oauth/v2/accessToken"