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



我在连接到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">

最新更新