1
votes

I'm building an app that I would use to make posts to LinkedIn. I'm using Visual Studio 2017 (asp.net mvc c#). I have already created an app in LinkedIn with Client ID and Client Secret. I have also create an async Function GetCode() my application in Visual Studio.

This function makes a request to this url: string url = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=kjahldj9384&state=DCEEFWKDHIUs5dffef424&redirect_uri=http://localhost:51272/";

When i run the application and this function gets called, I'm redirected to the LinkedIn Authentication page to login. But when I enter the LinkedIn login credentials and hit login, it takes me to this page: https://www.linkedin.com/uas/login-submit which displays the error message:

Request Error We’re sorry, there was a problem with your request. Please make sure you have cookies enabled and try again.

Or follow this link to return to the home page. This is my GetCode function:

        //Get LinkedIn Code
    [HttpGet]
    [Route("api/LinkedIn/GetCode")]
    public async Task<HttpResponseMessage> GetCode()
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Content-Type", "application/x-www-form-urlencoded");

     string url = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=54565kggfh&state=DCEEFW55754FD5dffef424&redirect_uri=http://localhost:57313/";

            HttpResponseMessage apiResponseMsg = await client.GetAsync(new Uri(url));

            return apiResponseMsg;
        }
    }

Is there something I'm doing wrong?

1

1 Answers

1
votes

First, make sure to add your redirect_uri to OAuth 2.0 Settings in the Linkedin developer portal (under Auth tab). If that isn't the issue, try below.

If you build the same URL, and redirect the page to it instead of using a response message, then you should be sent back to your website with the code as a query parameter (http://localhost:60137/?code=xxxxxxxxxxxxx). I made a working example with a test application:

    [HttpGet]
    public ActionResult GetCode()
    {
        //build url from config file
        string url = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=78z99sg1ncgbma&redirect_uri=http://localhost:60137";

        return new RedirectResult(url);
    }

Hope this helps.