Search code examples
linkedin-apitls1.2webrequest

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


I have suddenly started receiving the following error when connecting to the LinkedIn oAuth2 API... The underlying connection was closed: An unexpected error occurred on a send.

This is normally caused by ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 not being set.

The line of code that errors is

Dim oStream As Stream = oWebRequest.GetRequestStream()

I have set this in code;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

But continue getting the error. If I run the code locally it works fine but not on the deployed server. The server is Windows 2012 R2. I have disabled all versions of SSL / TLS prior to TLS 1.2 on the server and performed a restart of the server but still the error persists even with ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 set in the code.

The code runs locally but I still receive the error on the server and I am out of ideas!

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

Solution

  • Try changing sAuthServiceUrl = "https://www.linkedin.com/oauth/v2/accessToken" to sAuthServiceUrl = "https://api.linkedin.com/oauth/v2/accessToken"