Commit 0b6fd6d3 authored by DANIEL DIAZ SANCHEZ's avatar DANIEL DIAZ SANCHEZ
Browse files

Update README.md

parent 7544044b
......@@ -64,6 +64,126 @@ Esto nos dará un JSON de este tipo:
[{"retweeted_status":{"contributors":null,"text":"We have disabled SSLv3 protocol support in response to the vulnerability published today. You may need to update your browser to use Twitter","geo":null,"retweeted":false,"in_reply_to_screen_name":null,"truncated":false,"lang":"en","entities":{"symbols":[],"urls":[],"hashtags":[],"user_mentions":[]},"in_reply_to_status_id_str":null,"id":522190947782643712,"source":"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client<\/a>","in_reply_to_user_id_str":null,"favorited":false,"in_reply_to_status_id":null,"retweet_count":695,"created_at":"Wed Oct 15 01:03:18 +0000 2014","in_reply_to_user_id":null,"favorite_count":226,"id_str":"522190947782643712","place":null,"user":{"location":"Twitter HQ","default_profile":false,"profile_background_tile":true,"statuses_count":40,"lang":"en","profile_link_color":"009999","id":1137751093,"following":false,"protected":false,"favourites_count":0,"profile_text_color":"333333","description":"The Product Security Team at Twitter.","verified":true,"contributors_enabled":false,"profile_sidebar_border_color":"EEEEEE","name":"Twitter Security","profile_background_color":"131516","created_at":"Thu Jan 31 19:37:42 +0000 2013","is_translation_enabled":false,"default_profile_image":false,"followers_count":13852,"profile_image_url_https":"https://pbs.twimg.com/profile_images/3321149170/6bf0ef1ae272b203acfdee4d7e61df49_normal.png","geo_enabled":false,"profile_background_image_url":"http://abs.twimg.com/images/themes/theme14/bg.gif","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme14/bg.gif","follow_request_sent":false,"entities":{"description":{"urls":[]},"url":{"urls":[{"expanded_url":"https://twitter.com/about/security","indices":[0,23],"display_url":"twitter.com/about/security","url":"https://t.co/E9FCUUoXKF"}]}},"url":"https://t.co/E9FCUUoXKF","utc_offset":-25200,"time_zone":"Pacific Time (US & Canada)","notifications":false,"profile_use_background_image":true,"friends_count":0,"profile_sidebar_fill_color":"EFEFEF","screen_name":"twittersecurity","id_str":"1137751093","profile_image_url":"http://pbs.twimg.com/profile_images/3321149170/6bf0ef1ae272b203acfdee4d7e61df49_normal.png","listed_count":188,"is_translator":false},"coordinates":null},"contributors":null,"text":"RT @twittersecurity: We have disabled SSLv3 protocol support in response to the vulnerability published today. You may need to update your \u2026","geo":null,"retweeted":false,"in_reply_to_screen_name":null,"truncated":false,"lang":"en","entities":{"symbols":[],"urls":[],"hashtags":[],"user_mentions":[{"id":1137751093,"name":"Twitter Security","indices":[3,19],"screen_name":"twittersecurity","id_str":"1137751093"}]},"in_reply_to_status_id_str":null,"id":522339641815748608,"source":"<a href=\"http://itunes.apple.com/us/app/twitter/id409789998?mt=12\" rel=\"nofollow\">Twitter for Mac<\/a>","in_reply_to_user_id_str":null,"favorited":false,"in_reply_to_status_id":null,"retweet_count":695,"created_at":"Wed Oct 15 10:54:09 +0000 2014","in_reply_to_user_id":null,"favorite_count":0,"id_str":"522339641815748608","place":null,"user":{"location":"San Francisco, CA","default_profile":false,"profile_background_tile":true,"statuses_count":3520,"lang":"en","profile_link_color":"0084B4","profile_banner_url":"https://pbs.twimg.com/profile_banners/6253282/1347394302","id":6253282,"following":false,"protected":false,"favourites_count":26,"profile_text_color":"333333","description":"The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.","verified":true,"contributors_enabled":false,"profile_sidebar_border_color":"C0DEED","name":"Twitter API","profile_background_color":"C0DEED","created_at":"Wed May 23 06:01:13 +0000 2007","is_translation_enabled":false,"default_profile_image":false,"followers_count":2418318,"profile_image_url_https":"https://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png","geo_enabled":true,"profile_background_image_url":"http://pbs.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png","profile_background_image_url_https":"https://pbs.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png","follow_request_sent":false,"entities":{"description":{"urls":[]},"url":{"urls":[{"expanded_url":"http://dev.twitter.com","indices":[0,22],"display_url":"dev.twitter.com","url":"http://t.co/78pYTvWfJd"}]}},"url":"http://t.co/78pYTvWfJd","utc_offset":-25200,"time_zone":"Pacific Time (US & Canada)","notifications":false,"profile_use_background_image":true,"friends_count":48,"profile_sidebar_fill_color":"DDEEF6","screen_name":"twitterapi","id_str":"6253282","profile_image_url":"http://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png","listed_count":12846,"is_translator":false},"coordinates":null},{"contributors":null,"text":"The OAuth / xAuth issue affecting REST endpoints has been resolved. Please visit our OAuth forums for support https://t.co/XiKCrlfCNX","geo":null,"retweeted":false,"in_reply_to_screen_name":"twitterapi","possibly_sensitive":false,"truncated":false,"lang":"en","entities":{"symbols":[],"urls":[{"expanded_url":"https://twittercommunity.com/category/oauth","indices":[110,133],"display_url":"twittercommunity.com/category/oauth","url":"https://t.co/XiKCrlfCNX"}],"hashtags":[],"user_mentions":[]},"in_reply_to_status_id_str":"515255192787230720","id":515288036242763776,"source":"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client<\/a>","in_reply_to_user_id_str":"6253282","favorited":false,"in_reply_to_status_id":515255192787230720,"retweet_count":79,"created_at":"Thu Sep 25 23:53:36 +0000 2014","in_reply_to_user_id":6253282,"favorite_count":55,"id_str":"515288036242763776","place":null,"user":{"location":"San Francisco, CA","default_profile":false,"profile_background_tile":true,"statuses_count":3520,"lang":"en","profile_link_color":"0084B4","profile_banner_url":"https://pbs.twimg.com/profile_banners/6253282/1347394302","id":6253282,"following":false,"protected":false,"favourites_count":26,"profile_text_color":"333333","description":"The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.","verified":true,"contributors_enabled":false,"profile_sidebar_border_color":"C0DEED","name":"Twitter API","profile_background_color":"C0DEED","created_at":"Wed May 23 06:01:13 +0000 2007","is_translation_enabled":false,"default_profile_image":false,"followers_count":2418318,"profile_image_url_https":"https://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png","geo_enabled":true,"profile_background_image_url":"http://pbs.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png","profile_background_image_url_https":"https://pbs.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png","follow_request_sent":false,"entities":{"description":{"urls":[]},"url":{"urls":[{"expanded_url":"http://dev.twitter.com","indices":[0,22],"display_url":"dev.twitter.com","url":"http://t.co/78pYTvWfJd"}]}},"url":"http://t.co/78pYTvWfJd","utc_offset":-25200,"time_zone":"Pacific Time (US & Canada)","notifications":false,"profile_use_background_image":true,"friends_count":48,"profile_sidebar_fill_color":"DDEEF6","screen_name":"twitterapi","id_str":"6253282","profile_image_url":"http://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png","listed_count":12846,"is_translator":false},"coordinates":null}]
```
## Descarga de tweets programática
### Oauth
Oauth permite delegar el uso de tu cuenta de twitter (o de Facebook o de google...) a una aplicación, de forma que pueda hacer uso de ella **en tu nombre**. Para hacer eso, es necesario incluir una firma generada mediante OAuth en todas las peticiones HTTP que haga tu aplicación al API. Lee detenidamente las siguientes instrucciones para usar OAUTH y permitir que el usuario delegue a la aplicación el uso (read/write) de tu cuenta de twitter aunque nosotros usaremos la última de todas (application only):
* Información de la aplicación: https://apps.twitter.com/
* Overview: https://dev.twitter.com/oauth/overview
* Autenticación por PIN: https://dev.twitter.com/oauth/pin-based
* Autenticación 3-Legged Oauth: https://dev.twitter.com/oauth/3-legged
* Application only https://dev.twitter.com/oauth/application-only
Vamos a usar un Bearer Token dado que sólo queremos acceso read only a la cuenta para leer tweets. El esquema es el siguiente:
<img src="https://g.twimg.com/dev/documentation/image/appauth_0.png" width="500">
### Crear proyecto y descargar dependencias
Crea un proyecto en Java. Transformalo en Maven (botón derecho sobre el proyecto > Configure > Convert to Maven Project).
Añade las dependencias:
```xml
<dependencies>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
```
### Obtención del Bearer Token
Según consta en twitter, para obtener un bearer token es necesario:
1. codificar en forma de URL ([URLEncode](https://en.wikipedia.org/wiki/Percent-encoding)) tanto el consumer key como el consumer secret.
2. unir ambos en una string separándolos por ":"
3. codificar el resultado con base64
4. hacer una petición a "https://api.twitter.com/oauth2/token" añadiendo una cabecera con el formato Authorization: Basic {Credenciales codificadas}
Para conseguirlo, crea un paquete y una clase dentro de ese paquete con este código:
```java
package cdistRest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.commons.codec.binary.Base64;
import com.google.gson.Gson;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.body.RequestBodyEntity;
public class TwitterCrawler {
private static final String oauth_consumer_key = "XXXXXXXXx";
private static final String oauth_consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
public class BearerToken
{
private String access_token;
private String token_type;
}
public static String deserializeJson(String input_json)
{
Gson gson = new Gson();
BearerToken new_bearerToken = gson.fromJson(input_json, BearerToken.class);
return new_bearerToken.access_token;
}
public static void main(String args[]) throws UnsupportedEncodingException {
try {
/* get bearer token according to https://dev.twitter.com/oauth/application-only */
String URLEncoderConsumerKey = URLEncoder.encode(oauth_consumer_key, "UTF-8");
String URLEncoderConsumerSecret = URLEncoder.encode(oauth_consumer_secret, "UTF-8");
String AuthorizationHeader = URLEncoderConsumerKey+":"+URLEncoderConsumerSecret;
String AuthorizationHeaderB64 = Base64.encodeBase64String(AuthorizationHeader.getBytes("UTF8"));
System.out.println(AuthorizationHeaderB64);
RequestBodyEntity postReq = Unirest.post("https://api.twitter.com/oauth2/token")
.header("User-Agent","TwitterApp")
.header("host", "api.twitter.com")
.header("Authorization", "Basic " + AuthorizationHeaderB64)
.header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
.body("grant_type=client_credentials");
HttpResponse<String> res = postReq.asString();
System.out.println("Bearer Token : " + TwitterCrawler.deserializeJson(res.getBody()));
/* we now have a brearer token so can make requests to Twitter */
} catch (UnirestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
```
Sustituye `oauth_consumer_key` y `oauth_consumer_secret` por los valores de tu aplicación y pruébalo.
### Anade una consulta al API de Twetter
Vamos a probar con la función del API [get user timeline](https://dev.twitter.com/rest/reference/get/statuses/user_timeline) que permite obtener los tweets más recientes de un usuario.
## Importa el proyecto completo en Eclipse desde este repositiorio
El eclipse que usamos dispone de un plugin de Git. Para importar ese proyecto (así como otros que veremos en clase) sigue este manual:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment