LinkedIn Sign-In, getting issue to fetch access token with authorization code

Dev
Dev
Member
40 Points
5 Posts

I'm trying to implement Sign-in with LinkedIn in ASP.Net Mvc application. I'm able to get authorization code successfully but having issue to getting access token. I'm trying with following code:

public async Task<string> GetAccessToken(string authorizationCode)
{
    var inputParams = new Dictionary<string, string>
    {
        { "grant_type", "authorization_code" },
        { "code", authorizationCode },
        { "redirect_uri", "http://localhost:1938/Account/LinkedinOauthCallback" },
        { "client_id", _linkedInSettings.AppKey },
        { "client_secret", _linkedInSettings.AppSecret }
    };

    var content = new FormUrlEncodedContent(inputParams);
    content.Headers.Clear();
    content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    string accessToken = "";

    using (HttpClient client = new HttpClient())
    {
        //client.DefaultRequestHeaders.Accept.Clear();
        //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

        HttpResponseMessage response = await client.PostAsync("https://www.linkedin.com/oauth/v2/accessToken", content);
        var accessTokenResponse = await response.Content.ReadAsAsync<LinkedInAccessTokenResponse>();
        accessToken = accessTokenResponse.access_token;
    }

    return accessToken;
}

I'm not getting any response, getting unresponsive.

 

Views: 512
Total Answered: 1
Total Marked As Answer: 0
Posted On: 01-Jan-2021 22:49

Share:   fb twitter linkedin
Answers
Dev
Dev
Member
40 Points
5 Posts
         

I'm not sure what's happening. Able to get access token by making it sync request from local. 

public string GetAccessToken(string authorizationCode)
{
    var inputParams = new Dictionary<string, string>
    {
        { "grant_type", "authorization_code" },
        { "code", authorizationCode },
        { "redirect_uri", "http://localhost:1938/Account/LinkedinOauthCallback" },
        { "client_id", _linkedInSettings.AppKey },
        { "client_secret", _linkedInSettings.AppSecret }
    };

    var content = new FormUrlEncodedContent(inputParams);
    content.Headers.Clear();
    content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    string accessToken = "";

    using (HttpClient client = new HttpClient())
    {
        //client.DefaultRequestHeaders.Accept.Clear();
        //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

        HttpResponseMessage response = await client.PostAsync("https://www.linkedin.com/oauth/v2/accessToken", content).Result;
        var accessTokenResponse = await response.Content.ReadAsAsync<LinkedInAccessTokenResponse>().Result;
        accessToken = accessTokenResponse.access_token;
    }

    return accessToken;
}

But now getting error on server:

System.Net.Http.HttpRequestException: 
An error occurred while sending the request. --->
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. --->
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. --->
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
Posted On: 07-Jan-2021 07:14
 Log In to Chat