Table of Contents
Introduction
Getting started
I would suggest you to first clone the Access Facebook Data Tutorial as it does have required page structure and dependencies
Add Maven Dependencies
Add the spring-social-google dependency
1 2 3 4 5 | org.springframework.social spring-social-linkedin 1.0.0.RELEASE |
Update the application.properties
Now update the application.properties with your secret and client key
1 2 3 4 | spring.social.linkedin.app-id=771mrzk94hye1w spring.social.linkedin.app-secret=iIJFgBf9lCb18zYe |
Add the HTML for LinkedIn
Under src/main/resources/templates/connect you should have facebookConnect.html and facebookConnected.html, if you have cloned/downloaded the spring tutorial properly.
Again replace facebook with linkedin in HTML and save them as linkedinConnected.html and linkedinConnect.html or copy them as shown below. Make sure the scope is properly defined in your request.
1 2 3 | <input type="hidden" name="scope" value="r_basicprofile,r_emailaddress" /> |
linkedinConnect.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <html> <head> <title>Hello LinkedIn</title> </head> <body> <h3>Connect to Linkedin</h3> <form action="/connect/linkedin" method="POST"> <input type="hidden" name="scope" value="r_basicprofile,r_emailaddress" /> <div class="formInfo"> <p>You aren't connected to LinkedIn yet. Click the button to connect this application with your account.</p> </div> <p><button type="submit">Connect to LinkedIn</button></p> </form> </body> </html> |
linkedinConnected.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <html> <head> <title>Hello Facebook</title> </head> <body> <h3>Connected to LinkedIn</h3> <p> You are now connected to your LinkedIn account. Click <a href="/linkedin">here</a> to see some entries from your LinkedIn feed. </p> </body> </html> |
Add a @Controller for LinkedIn
We need to add a rest controller so it can handle LinkedIn authorization requests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | @Controller @RequestMapping("/linkedin") public class LinkedInController { private LinkedIn linkedIn; private ConnectionRepository connectionRepository; public LinkedInController(LinkedIn linkedIn, ConnectionRepository connectionRepository) { this.linkedIn = linkedIn; this.connectionRepository = connectionRepository; } @GetMapping public String helloFacebook(Model model) { if (connectionRepository.findPrimaryConnection(LinkedIn.class) == null) { return "redirect:/connect/linkedin"; } ProfileOperations user = linkedIn.profileOperations(); System.out.println(user); model.addAttribute("linkedInProfile",linkedIn.profileOperations().getUserProfileFull()); return "linkedin"; } } |
Add HTML to show user details
In src/main/resources add a file called “linkedin.html” at same level as “hello.html”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <html> <head> <title>Hello Facebook</title> </head> <body> <h3>Hello, <span th:text="${linkedInProfile.firstName}"></span><span th:text="${linkedInProfile.lastName}"></span></h3> <h4><span th:text="${linkedInProfile.emailAddress}"></span></h4> <h4><span th:text="${linkedInProfile.id}"></span></h4> <h4><span th:text="${linkedInProfile.location.country}"></span></h4> </body> </html> |
That’s all
Run the application on localhost: port/linkedin and you should be authenticated. Drop comments if you face any problem
If you want to change the default page flow (linkedinconnect.tml to connect/linkedinConnected.html),please follow below link