When accessing the previous URL, click on Create Application and you will be redirected to the following page which will ask you for basic information about the application being created:
Unlike many other OAuth 2.0 Providers, LinkedIn requires an application logo as you might have seen in the previous image. LinkedIn also asks for more business data such as the website URL, business email, and business phone.
Fill out the form and click on the Submit button. You will be redirected to the application's dashboard as shown in the following screenshot which presents you with the Authentication Keys and the field to define the redirection URL:
As we are using Spring Social, let's add a Redirect URL which follows the pattern regarding the endpoint which was defined as connect/linkedin. After entering the Redirect URL, click on the Add and then click on Update button.
Now, make sure to grab the Authorization Keys (that is, client_id and client_secret) to use in the application that we will create in the next step.
Create the initial project using Spring Initializr as we did for the other recipes in this book. Go to https://start.spring.io/ and define the following data:
Set up the Group as com.packt.example
Define the Artifact as social-linkd(you can use different names if you prefer, but do not forget to change all the references forlinkd that were used throughout this recipe)
Add WebandThymeleafas the dependencies for this project
Import the project to your IDE (if using Eclipse, import as a Maven Project).
For this recipe, the Spring Social provider implementation already provides a well-defined auto configuration support for Spring Boot. So, it's easier to create the application and just having to worry about the client credential settings. Open the application.properties file and add the following content (using the credentials generated for your application):
Now create the controller class ProfileController which has the responsibility of retrieving the user's profile through the use of the LinkedIn API. This class should be created within the package com.packt.linkedin.example.sociallinkd.
Make sure the class ProfileController looks like the following:
@Controller
public class ProfileController {
@Autowired
private LinkedIn linkedin;
@Autowired
private ConnectionRepository connectionRepository;
@GetMapping
public String profile(Model model) {
if (connectionRepository.findPrimaryConnection(LinkedIn.class) == null) {
return "redirect:/connect/linkedin";
}
String firstName = linkedin.profileOperations() .getUserProfile().getFirstName();
model.addAttribute("name", firstName);
return "profile";
}
}
As you might expect, the application will be able to retrieve the user's profile only when the user has made the connection between LinkedIn and the social-linkd client application.
So, if there is no connection available, the user will be redirected to /connect/linkedin which is mapped by the ConnectController class from Spring Social. Such an endpoint will redirect the user to the view, defined by the name linkedinConnect which maps directly to the linkedinConnect.html file that might be created under templates/connect directory, located within the src/main/resources project directory as follows:
Looking at the previous screenshot, you can see that there is also linkedinConnected.html, which will be presented when a user's connection is available for the social-linkd application.
All the logic to decide when to present linkedinConnect.html or linkedinConnected.html is defined inside the method connectionStatus from the ConnectController class. The main logic is defined as presented in the following code:
Add the following HTML content to linkedinConnect.html:
<html>
<head><title>Social LinkedIn</title></head>
<body>
<h2>Connect to LinkedIn to see your profile</h2>
<form action="/connect/linkedin" method="POST">
<input type="hidden" name="scope" value="r_basicprofile" />
<div class="formInfo">
Click the button to share your profile with
<b>social-linkedin</b>
</div>
<p><button type="submit">Connect to LinkedIn</button></p>
</form>
</body>
</html>
Now add the following HTML content to linkedinConnected.html:
<html>
<head>
<title>Social LinkedIn</title>
</head>
<body>
<h2>Connected to LinkedIn</h2>
<p>Click <a href="/">here</a> to see your profile.</p>
</body>
</html>
To present the user's profile, create the file profile.html inside the templates directory with the following content: