0
votes

Looking at the https://developer.linkedin.com/docs/oauth2 I try here to get an access token.

What can be wrong with this piece of code ?

    OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
    TokenRequestBuilder r = OAuthClientRequest
            .tokenLocation("https://www.linkedin.com/oauth/v2/accessToken")
            .setCode(code)
            .setGrantType(GrantType.AUTHORIZATION_CODE)
            .setClientId(LinkedInClientID)
            .setClientSecret(LinkedInClientSecret)
            .setRedirectURI("http://localhost:8080/authenticatedLinkedIn");

    Map<String,String> m = new HashMap<String,String>();
    m.put("Content-Type", "application/x-www-form-urlencoded");

    int bodyLength = r.buildBodyMessage().getBody().length();
    System.out.println("Body l = " + bodyLength);
    m.put("Content-Length", Integer.toString(bodyLength));
    r.buildHeaderMessage().setHeaders(m);
    OAuthClientRequest request = r.buildQueryMessage();
    OAuthJSONAccessTokenResponse tk = oAuthClient.accessToken(request, OAuth.HttpMethod.POST);

I get the following error :

Server returned HTTP response code: 411 for URL: https://www.linkedin.com/oauth/v2/accessToken?code=AQSZfSXpQ6z3575474fhfbZmxJQofGiwtpw53Y1FnlALvKBWJgQKfJH8kvHM-3f5ZtOqndit594S2cmZrFuiNaXcBOHuSf8yMgFgr4uh-a40Ag&grant_type=authorization_code&client_secret=gHPiGTTyb1KKHPEP&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2FauthenticatedLinkedIn&client_id=86txkd469mat

1

1 Answers

2
votes

You need to send a POST request with params in body urlenconded... so

First change:

OAuthClientRequest request = r.buildQueryMessage();

With:

OAuthClientRequest request = r.buildBodyMessage();

Then you do not need to put any header Oltu will manage this for U.

Hope it will help.