Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 330a770

Browse files
Tutorial Spring Social Twitter
Uso de la API de Twitter en una aplicación web desarrollada con Spring Boot.
1 parent 1e2c21d commit 330a770

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
7+
<modelVersion>4.0.0</modelVersion>
8+
9+
<groupId>tutor.spring</groupId>
10+
<artifactId>spring-social-twitter</artifactId>
11+
<version>1.0.0</version>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>1.5.6.RELEASE</version>
17+
</parent>
18+
19+
<dependencies>
20+
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.social</groupId>
28+
<artifactId>spring-social-twitter</artifactId>
29+
</dependency>
30+
31+
</dependencies>
32+
33+
<properties>
34+
<java.version>1.8</java.version>
35+
</properties>
36+
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-maven-plugin</artifactId>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package hello;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.social.connect.ConnectionRepository;
7+
import org.springframework.social.twitter.api.Tweet;
8+
import org.springframework.social.twitter.api.Twitter;
9+
import org.springframework.social.twitter.api.TwitterProfile;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.ui.Model;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
14+
@Controller
15+
public class TwitterController {
16+
17+
@Autowired
18+
private Twitter twitter;
19+
20+
@Autowired
21+
private ConnectionRepository connectionRepository;
22+
23+
@RequestMapping("/")
24+
public String tweets(Model model) {
25+
26+
if (connectionRepository.findPrimaryConnection(Twitter.class) == null) {
27+
return "redirect:/connect/twitter";
28+
}
29+
30+
List<Tweet> tweets = twitter.timelineOperations().getHomeTimeline();
31+
TwitterProfile profile = twitter.userOperations().getUserProfile();
32+
33+
model.addAttribute("tweets", tweets);
34+
model.addAttribute("profile", profile);
35+
36+
return "tweetsPage";
37+
}
38+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.social.twitter.appId=xxxxxxxxxxxxxxxxxxxxxxxxxx
2+
spring.social.twitter.appSecret=xxxxxxxxxxxxxxxxxxxxxxxxxx
3+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<html>
2+
<head>
3+
<title>Hello Twitter</title>
4+
</head>
5+
<body>
6+
<h3>Connect to Twitter</h3>
7+
<span th:if="${social_provider_error}">Provider error (asegurate con configurar el <b>appId</b> y <b>consumerKey</b>)</span>
8+
<form action="/connect/twitter" method="POST">
9+
<div class="formInfo">
10+
<p>Aun no estas conectado a Twitter, presiona el boton para conectar el App con tu cuenta.</p>
11+
</div>
12+
<p><button type="submit">Connect to Twitter</button></p>
13+
</form>
14+
</body>
15+
</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>Hello Twitter</title>
4+
</head>
5+
<body>
6+
<h3>Connected to Twitter</h3>
7+
8+
<p>
9+
App conectada a tu cuenta de Twitter. <br />
10+
<a href="/">Ir al App</a> y ver mis tweets.
11+
</p>
12+
</body>
13+
</html>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Spring Twitter</title>
5+
<style type="text/css">
6+
.tweet-container {
7+
border: gray solid 1px;
8+
border-radius: 3px;
9+
margin: 20px 10px;
10+
padding: 10px;
11+
box-shadow: #c3c3c3 2px 2px 5px 0px;
12+
}
13+
14+
body { font-family: Roboto; }
15+
16+
</style>
17+
</head>
18+
<body>
19+
20+
<h3>Hola, <span th:text="${profile.name}">Usuario</span></h3>
21+
22+
<h4>Tweets List</h4>
23+
24+
<ul>
25+
<li th:each="tweet : ${tweets}" style="list-style: none;">
26+
<div class="tweet-container">
27+
<img th:src="${tweet.user.profileImageUrl}" style="display: block; margin-bottom: 10px;"/>
28+
<span th:text="${tweet.user.name}">Username</span>
29+
<p th:text="${tweet.text}">Tweet</p>
30+
</div>
31+
</li>
32+
</ul>
33+
34+
</body>
35+
</html>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /