2

I have been working on integrating my application with LinkedIn by following the documentation located here. I have created my application in LinkedIn and am able to successfully retrieve the authorization code but I am getting the following error when trying to get the access token :

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

I have verified the following to be true:

  1. The redirect uri is the same for the authorization request and access token request
  2. I am using the authorization code within 20 seconds of it being issued.
  3. I am including "Content-Type:application/x-www-form-urlencoded" in the request header

Some things I have tried with no success:

  1. Posting the request with the parameters as part of the url
  2. Posting the request with parameters as part of the body
  3. sending the redirect uri as both encoded and plain text
  4. using a get request instead of a post.

Here is my current code:

linkedin_access_token_url = "https://www.linkedin.com/uas/oauth2/accessToken?"+
                "grant_type=authorization_code"+
                "&code="+ authCode
                + "&redirect_uri=https://localhost:8090/ProfileSetup/linkedInAuth.jsp
                + "&client_id=" + linkedin_client_id
                + "&client_secret=" + linkedin_client_secret;
        HttpClient http = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(linkedin_access_token_url);
        try {
            httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            HttpResponse response = http.execute(httppost);
            HttpEntity entity = response.getEntity();
            System.out.println("status code " +
            response.getStatusLine().getStatusCode()); 
            System.out.println("statusreason"+
            response.getStatusLine().getReasonPhrase());
            InputStream stream = entity.getContent();
            StringBuilder sb = new StringBuilder();
            String line;
            try {
                br = new BufferedReader(new InputStreamReader(stream));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
            } catch (Exception e) {
            }
            String resp = sb.toString();
            System.out.println("response " + resp);
        } catch (Exception ex) {
            System.out.println("linked in  HttpResponse Error: " + ex);
        } finally {
            httppost.releaseConnection();
        }

And the authorization url (actual client id is sent in place of linkedin_client_id):

https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=linkedin_client_id&redirect_uri=https://localhost:8090/ProfileSetup/linkedInAuth.jsp&state=0kcmjj5504tpgb9&scope=r_basicprofile

Does anyone see what I am doing wrong? If I take this compiled url and paste it in the browser, I am able to retrieve an access token without any issue. Is there a problem with my request?

Cœur
  • 37,241
  • 25
  • 195
  • 267
kmb71123
  • 21
  • 1
  • Since it is working vi direct url, have you tried to change your request from post to get to see what happens? – Jorge Campos Jan 06 '16 at 15:48
  • I have tried a get request with the same result. – kmb71123 Jan 06 '16 at 16:01
  • Are you confident you've configured https://localhost:8090/ProfileSetup/linkedInAuth.jsp as a valid redirect target in your LinkedIn app's configuration at https://www.linkedin.com/developer/apps? – Justin Kominar Jan 06 '16 at 17:12
  • Yes, I have verified that the url is listed under Authorized Redirect URLs on linkedIn. It works to get the authorization code and if i paste the compiled url in my browser I receive the correct access token json. – kmb71123 Jan 06 '16 at 20:17

2 Answers2

0

Looks like you are including all of the parameters for the request as attributes in the URL, rather than in the POST body as x-www-form-urlencoded elements.

Check out this other Stack thread for details on how to send the values in the request body rather than as URL attributes:

Sending HTTP POST Request In Java

Community
  • 1
  • 1
Justin Kominar
  • 3,346
  • 1
  • 14
  • 14
0

I was finally able to figure out what was wrong with my app. My local environment was not configured correctly for https. Moving the code onto our dev box set up with https fixed the issue.

kmb71123
  • 21
  • 1