Spring > Social networks

Facebook

Guides

POM

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project ...>
 
    <!-- ... -->
 
    <dependencies>
        <!-- ... -->
        <dependency> <!-- If you want to access Facebook -->
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-facebook</artifactId>
        </dependency>
        <dependency> <!-- If you want to generate web pages -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency> <!-- If you want to parse JSON -->
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency> <!-- If you want to deal with security tokens -->
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-crypto</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>
    </dependencies>
 
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
 
</project>

Accessing Facebook Data

Passing Application ID/Secret to the application

Don't forget to add “http://localhost:8080/” as a Platform (type=Website) in your application settings page (https://developers.facebook.com/)

src/main/resources/application.properties
spring.social.facebook.appId=962223610477458
spring.social.facebook.appSecret=b7dfec28b08ac4e8c2a09cbac4662c15

The presence of these properties and Spring Social Facebook in the classpath will trigger automatic configuration of Spring Social’s ConnectController, FacebookConnectionFactory, and other components of Spring Social’s connection framework.

Establish connection

ConnectController deals with redirection to/from Facebook and establish/show connection via /connect. Views are named connect/{providerId}Connect (no connection) and connect/{providerId}Connected (connected). Here, {providerId} = facebook.

When no connection
src/main/resources/templates/connect/facebookConnect.html
<html>
	<head>
		<title>Hello Facebook</title>
	</head>
	<body>
		<h3>Connect to Facebook</h3>
 
		<form action="/connect/facebook" method="POST">
			<input type="hidden" name="scope" value="user_posts" />
			<div class="formInfo">
				<p>You aren't connected to Facebook yet. Click the button to connect this application with your Facebook account.</p>
			</div>
			<p><button type="submit">Connect to Facebook</button></p>
		</form>
	</body>
</html>

It POSTS to /connect/facebook, which is handled by ConnectController and will kick off the OAuth authorization code flow.

When a connection exists
src/main/resources/templates/connect/facebookConnected.html
<html>
	<head>
		<title>Hello Facebook</title>
	</head>
	<body>
		<h3>Connected to Facebook</h3>
 
		<p>
			You are now connected to your Facebook account.
			Click <a href="/">here</a> to see some entries from your Facebook feed.
		</p>
	</body>
</html>

Access data

src/main/java/hello/HelloController.java
package hello;
 
import javax.inject.Inject;
 
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.PagedList;
import org.springframework.social.facebook.api.Post;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
@RequestMapping("/")
public class HelloController {
 
    private Facebook facebook;
	private ConnectionRepository connectionRepository;
 
    // We inject the reference to Spring Social’s Facebook API binding (Facebook object)   
    @Inject
    public HelloController(Facebook facebook, ConnectionRepository connectionRepository) {
        this.facebook = facebook;
	this.connectionRepository = connectionRepository;
    }
 
    @RequestMapping(method=RequestMethod.GET)
    public String helloFacebook(Model model) {
        // If we're not logged in --> Redirected to login page.
        if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
            return "redirect:/connect/facebook";
        }
        // We store the infos in the model
        model.addAttribute("facebookProfile", facebook.userOperations().getUserProfile());
        PagedList<Post> feed = facebook.feedOperations().getFeed();
        model.addAttribute("feed", feed);
        // We return the name of the view that will display those infos
        return "hello";
    }
 
}
src/main/resources/templates/hello.html
<html>
	<head>
		<title>Hello Facebook</title>
	</head>
	<body>
		<h3>Hello, <span th:text="${facebookProfile.name}">Some User</span>!</h3>
 
		<h4>Here is your feed:</h4>
 
		<div th:each="post:${feed}">
			<b th:text="${post.from.name}">from</b> wrote:
			<p th:text="${post.message}">message text</p>
			<img th:if="${post.picture}" th:src="${post.picture}"/>
			<hr/>
		</div>
	</body>
</html>
src/main/java/hello/Application.java
package hello;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
 
}
Print/export