Search code examples
oauthoauth-2.0linkedin-apilinkedin-jsapi

Linked in Javascript SDK access token


I'm using the LinkedIn Javascript SDK on my website in order to allow users to register and log in via Linked In.

I want to achieve this without redirecting the user to Linked In, but instead via the popup window.

The issue is, I'm not sure how to securely get an access token, which I can then use to get the users profile and create an account.

Currently, via the Javascript SDK, I can log the user in without redirection, and receive a oauth_token, which I then post to by backend. My Here is my code:

IN.User.authorize(function(){
    $.ajax({
      type: 'POST',
      url: '/auth/li/'+IN.ENV.auth.oauth_token,
    });
});

This succesfuly sends the token to my server, but how do I use it to then get an access token in order to use in the backend?

I tried to simply convert it to one using the LinkedIn API with

https://www.linkedin.com/oauth/v2/accessToken

but I get an error

Unable to retrieve access token: appid/redirect uri/code verifier does not match authorization code. Or authorization code expired.

I sort of understand the error, as with the javascript SDK there is no redirect URI.

With the Facebook SDK/API, on successful authorisation via the Javascript API, a cookie encrypted with my app secret is stored, and my backend can decrypt and read it, and then finally create an account.

Is there something like that in LinkedIn that I missed?


Solution

  • I have come up with a solution. I ended up not using the javascript API. What I did was generate a login URL on my server and retrieved it via AJAX. I then created a javascript popup window and aimed it at the login url. One the user authenticates, linkedin redirects to my selected redirect URL within the popup, and I can then authenticate on my side (and close the popup).