Skip to content

Commit e1cda13

Browse files
committed
Tutorial Spring Social Facebook
Acceso a la API de Facebook desde una aplicación web desarrollada con Spring Boot.
1 parent 330a770 commit e1cda13

File tree

7 files changed

+177
-0
lines changed

7 files changed

+177
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.springframework</groupId>
7+
<artifactId>spring-facebook</artifactId>
8+
<version>0.1.0</version>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.5.6.RELEASE</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.springframework.social</groupId>
24+
<artifactId>spring-social-facebook</artifactId>
25+
<version>3.0.0.M3</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.security</groupId>
30+
<artifactId>spring-security-crypto</artifactId>
31+
</dependency>
32+
</dependencies>
33+
34+
<properties>
35+
<java.version>1.8</java.version>
36+
</properties>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-maven-plugin</artifactId>
43+
</plugin>
44+
</plugins>
45+
</build>
46+
47+
<repositories>
48+
<repository>
49+
<id>spring-snapshots</id>
50+
<name>Spring Snapshots</name>
51+
<url>https://repo.spring.io/libs-milestone</url>
52+
<snapshots>
53+
<enabled>false</enabled>
54+
</snapshots>
55+
</repository>
56+
</repositories>
57+
<pluginRepositories>
58+
<pluginRepository>
59+
<id>spring-snapshots</id>
60+
<name>Spring Snapshots</name>
61+
<url>https://repo.spring.io/libs-milestone</url>
62+
<snapshots>
63+
<enabled>false</enabled>
64+
</snapshots>
65+
</pluginRepository>
66+
</pluginRepositories>
67+
68+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package hello;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package hello;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.social.connect.ConnectionRepository;
5+
import org.springframework.social.facebook.api.Facebook;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
10+
@Controller
11+
public class HelloController {
12+
13+
private final Facebook facebook;
14+
private final ConnectionRepository connectionRepository;
15+
16+
@Autowired
17+
public HelloController(Facebook facebook, ConnectionRepository connectionRepository) {
18+
this.facebook = facebook;
19+
this.connectionRepository = connectionRepository;
20+
}
21+
22+
@RequestMapping("/")
23+
public String facebook(Model model) {
24+
25+
if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
26+
return "redirect:/connect/facebook";
27+
}
28+
29+
model.addAttribute("perfil", facebook.userOperations().getUserProfile());
30+
model.addAttribute("feed", facebook.feedOperations().getFeed());
31+
32+
return "hello";
33+
}
34+
35+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.social.facebook.appId=120696445248610
2+
spring.social.facebook.appSecret=6c777222f9e3dcb1bff304d04f1a57a9
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
<title>Connect Facebook</title>
4+
</head>
5+
<body>
6+
<h3>Connectar a Facebook</h3>
7+
8+
<form action="/connect/facebook" method="POST">
9+
<input type="hidden" name="scope" value="user_posts" />
10+
<div class="formInfo">
11+
<p>Aun no estás conectado a Facebook, presiona el botón para conectar con tu cuenta.</p>
12+
</div>
13+
<p><button type="submit">Conectar a Facebook</button></p>
14+
</form>
15+
</body>
16+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html>
2+
<head>
3+
<title>Spring Facebook</title>
4+
</head>
5+
<body>
6+
<h3>Conectado a Facebook</h3>
7+
8+
<p>
9+
Tu cuenta de Facebook está conectada a la App.
10+
Click <a href="/">aquí</a> para ver tus publicaciones y perfil.
11+
</p>
12+
</body>
13+
</html>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Spring Social Facebook</title>
5+
<style>
6+
body { font-family: Roboto Light; }
7+
8+
.feed-container
9+
{
10+
border: gray solid 1px;
11+
border-radius: 5px;
12+
margin: 30px 10px;
13+
padding: 10px;
14+
}
15+
</style>
16+
</head>
17+
<body>
18+
19+
<h3>Hola, <span th:text="${perfil.name}">usuario</span> !</h3>
20+
21+
<h4>Estas son tus publicaciones:</h4>
22+
23+
<div th:each="post:${feed}" class="feed-container">
24+
<b th:text="${post.from.name}">autor</b> publicó:
25+
<p th:text="${post.message}">mensaje</p>
26+
<img th:if="${post.picture}" th:src="${post.picture}"/>
27+
</div>
28+
29+
</body>
30+
</html>

0 commit comments

Comments
 (0)