1
votes

I'm trying to get access token from LinkedIn.

I'm follwing this URL https://developer.linkedin.com/documents/authentication

I am able to get an authorization code.

But when I'm passing the authorization code to this URL

 https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code      &code=AUTHORIZATION_CODE &redirect_uri=YOUR_REDIRECT_URI &client_id=YOUR_API_KEY &client_secret=YOUR_SECRET_KEY

I get an error in the below format

{"error":"invalid_request","error_description":"missing required parameters, includes an invalid parameter value, parameter more then once. : Unable to retrieve access token : appId or redirect uri does not match authorization code or authorization code expired"}

Any ideas? Thanks in advance.

3
The error seems reasonably self-explanatory - either your redirect URI doesn't match the authorization code or the code expired. Have you checked those two items? - Tim
Thanks for your reply. I checked both items. - user2500094

3 Answers

0
votes

This is because authorization code expires in 20 seconds. So you have to get the Access Token within that time frame.

0
votes

I got the same error as you. I also met the following conditions:

  • My request was a POST request.
  • My redirect_uri's were the same in /authorization and /accessToken calls.
  • The /accessToken call was executed immediately after receiving the authorization code, so it wouldn't expire.

What finally did the trick for me was revoking the access token generated on the application details page on https://www.linkedin.com/secure/developer.

This is an access token for oAuth 1.a and is not compatible with oAuth 2.0 on which the linkedIn api is currently running.
After revoking this access token I was able to get a new one with the /authorization and /accessToken calls.

0
votes

I see this is an older thread, however if it will help anyone, here is my working solution, working on MVC core 2.0 as of december 2018:

first, redirect to LinkedIn like this

var url = "https://" + Request.Host + "/Login/LoginLinkedIn";            
url = WebUtility.UrlEncode(url);    
var redirectLinkedIn = "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=*ClientId*&client_secret=*ClientSecret*&redirect_uri=" + url + "&state=*random required nummeric value*";            
return Redirect(redirectLinkedIn);

after that, you will receive the answer in your Login/LoginLinkedIn action (don't forget to specify this path in your app settings Authorized Redirect URLs).

There you will use this private method to get a dynamic object filled with user data

private dynamic GetLinkedInUser(string code)
{
    dynamic jresult;
    NameValueCollection parameters = new NameValueCollection {
        {"client_id", *ClientId*},
        {"client_secret", *ClientSecret*},
        {"grant_type", "authorization_code"},
        {"redirect_uri", "https://" + Request.Host + "/Login/LoginLinkedIn"},
        {"code", code}
    };
    WebClient client = new WebClient();
    byte[] result = client.UploadValues("https://www.linkedin.com/oauth/v2/accessToken", "POST", parameters);
    string response = System.Text.Encoding.Default.GetString(result);
    string accessToken = JsonConvert.DeserializeObject<dynamic>(response).access_token;

    WebRequest webReq = WebRequest.Create("https://api.linkedin.com/v1/people/~:(id,email-address,first-name,last-name)?format=json");
    webReq.Method = "GET";
    webReq.Headers.Add("Authorization","Bearer "+accessToken);
    HttpWebResponse webResponse = (HttpWebResponse)webReq.GetResponse();
    using (StreamReader reader = new StreamReader(webResponse.GetResponseStream())) {
        string objText = reader.ReadToEnd();
        jresult = JsonConvert.DeserializeObject<dynamic>(objText);
    }
    return jresult;
}

hope it helps someone :)