Linkedin API access token generation error

finally, i got the access token. The authorization code expires in 20 seconds, so the access token URL must be called immediately after generating the authorization code.


Well, I went through the same problem and here is the process which i went through to fix it.

STEP#1: Authentication:

  1. Firstly, the authentication API is to be hit to fetch the authentication token.
  2. For this, a URL with Encoded parameters is to be hit as a GET request.
  3. Example: https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=[your_client_id]&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Flinkedin%2Fcallback&scope=r_emailaddress
  4. Please note that here, the parameters are to be encoded programatically.
  5. My non-encoded callback URL is: http://localhost:8080/linkedin/callback
  6. Therefore, my encoded URL is: http%3A%2F%2Flocalhost%3A8080%2Flinkedin%2Fcallback

Once you hit this as a GET request, you will receive a callback with a code and an optional state parameter.

STEP#2: Getting Access Token:

There are three pre-requisites to this call:

  1. The call must be POST
  2. It must have a header Content-Type with value application/x-www-form-urlencoded
  3. The data must be sent in request body.
  4. The value of redirect_url MUST BE SAME as in the previous call.
  5. In my case, it was: http://localhost:8080/linkedin/callback

Now the trick here is, that the call in (STEP#1 Authentication) was a GET request. Therefore, the redirect_url had to be programatically encoded.

Since the second call for is POST and is also application/x-www-form-urlencoded encoded, therefore the request body parameters do not have to be explicitly encoded. So, in this case, the redirect_uri would be sent as-is (http://localhost:8080/linkedin/callback)

Here is a snapshot of my Access Token API via postman: enter image description here

Tags:

Linkedin