In this article we will see how we can implement LinkedIn login for your web application. We will use the JavaScript SDK but LinkedIn has more than one SDKs available for your language of choice.
The article consists of two parts. The first part we will be the implementation of the login process while the second part will about getting the required credentials.
The code consists of two <script>
tags The first contains the refernce to the LinkedIn library and the declaration of the API Key.
<script type="text/javascript" src="//platform.linkedin.com/in.js"> api_key: <LinkedIn API Key> </script>
The second tag has the required functionality. The liLogin()
function will called when the user clicks on the Login with LinkedIn button. There you define the scope of data that your application requires.
The getProfileData()
function is called after authentication and makes a second call to get the requested data. As you can see, you declare which data you want in your response.
<script> var liLogin = function() { // Setup an event listener to make an API call once auth is complete IN.UI.Authorize().params({"scope":["r_basicprofile", "r_emailaddress"]}).place(); IN.Event.on(IN, 'auth', getProfileData); } var getProfileData = function() { // Use the API call wrapper to request the member's basic profile data IN.API.Profile("me").fields("id,firstName,lastName,email-address,picture-urls::(original),public-profile-url,location:(name)").result(function (me) { var profile = me.values[0]; var id = profile.id; var firstName = profile.firstName; var lastName = profile.lastName; var emailAddress = profile.emailAddress; var pictureUrl = profile.pictureUrls.values[0]; var profileUrl = profile.publicProfileUrl; var country = profile.location.name; }); } </script>
To get a LinkedIn API Key go to LinkedIn Developers. Go to My Apps and then click Create Application.
After creating your Application go to Authentication. The Client ID is the API Key required in our example. Then, go to JavaScript and declare which domains will be using your application. You can add localhost but make sure to include the port number (if any).
Note: As mentioned in the documentation, the JavaScript SDK does is not compatible with iOS 5+. This means the above code might not work in iPad and iPhone.
“You need to authenticate in order to use ‘me’ or ‘~’ as a valid profile identifier”.
I’m getting this error..What should I do.?.
Perhaps you are calling getProfileData() directly? You should call liLogin(), and that function has getProfileData() as a callback.
Hey thanks … its worked well … but now I am searhing for getting the access token to the console.
Thanks
Take a look here.
If you put in the
console.debug("oauth token:" + IN.ENV.auth.oauth_token);
in thegetProfileData(...)
function you will get the token.Thank you your code is very usefull… Do you have any example using IN.User.logout(); … I got Refused to get unsafe header.. when I tried to use it
not working
Hi PanKaj,
The code here is for the v1 of the LinkedIn API which is now deprecated.