<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>KodeGeek &#187; twitter4j</title>
	<atom:link href="http://kodegeek.com/blog/tag/twitter4j/feed/" rel="self" type="application/rss+xml" />
	<link>http://kodegeek.com/blog</link>
	<description>Programación, fitness, interés geek</description>
	<lastBuildDate>Sun, 05 Feb 2012 19:12:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Enviando Tweets desde Java usando Twitter4J: ¿Como proteger consumerKey and consumerSecret?</title>
		<link>http://kodegeek.com/blog/2009/07/18/enviando-tweets-desde-java-usando-twitter4j-%c2%bfcomo-proteger-consumerkey-and-consumersecret/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=enviando-tweets-desde-java-usando-twitter4j-%25c2%25bfcomo-proteger-consumerkey-and-consumersecret</link>
		<comments>http://kodegeek.com/blog/2009/07/18/enviando-tweets-desde-java-usando-twitter4j-%c2%bfcomo-proteger-consumerkey-and-consumersecret/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 02:53:15 +0000</pubDate>
		<dc:creator>josevnz</dc:creator>
				<category><![CDATA[internet]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[kodegeek]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[programación]]></category>
		<category><![CDATA[stupidzombie]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[twitter4j]]></category>

		<guid isPermaLink="false">http://kodegeek.com/blog/?p=2001</guid>
		<description><![CDATA[Bueno, mientras buscaba como agregar soporte para Facebook en StupidZombie, me conseguí con un problema interesante. Twitter4J requiere que definamos consumerKey y consumerSecret en el objeto Twitter en cada llamada. Lo cual trae problemas, ya que si distribuimos esas claves entonces cualquiera puede hacerse pasar por nuestra aplicación (No es un problema en una aplicación [...]]]></description>
			<content:encoded><![CDATA[<p>Bueno, mientras buscaba como agregar soporte para Facebook en StupidZombie, me conseguí con un problema interesante. Twitter4J requiere que definamos <a href="http://kodegeek.com/blog/2009/07/05/como-enviar-tweets-desde-java-usando-twitter4j/">consumerKey y consumerSecret en el objeto Twitter en cada llamada</a>.</p>
<p>Lo cual trae problemas, ya que si distribuimos esas claves entonces cualquiera puede hacerse pasar por nuestra aplicación (No es un problema en una aplicación web pero si en una de escritorio). El problema es peor si la aplicación es OpenSource porque entonces un Script-Kiddie puede hacer de las suyas sin mucho esfuerzo.</p>

<div class="wp_codebox"><table><tr id="p20014"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p2001code4"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//twitter.setOAuthConsumer(consumerKey, consumerSecret);</span>
accessToken <span style="color: #339933;">=</span> twitter.<span style="color: #006633;">getOAuthAccessToken</span><span style="color: #009900;">&#40;</span>token, tokenSecret, pin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Lo que mas me preocupa es que <a href="http://apiwiki.twitter.com/Authentication">autenticación de escritorio</a> no funciona si no lo definimos. Por ejemplo, en el código que escribí la vez pasada esto es lo que ocurre si no incluimos la linea que esta comentada en el párrafo anterior:</p>

<div class="wp_codebox"><table><tr id="p20015"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p2001code5"><pre class="java" style="font-family:monospace;">Jul <span style="color: #cc66cc;">18</span>, <span style="color: #cc66cc;">2009</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">18</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">49</span> PM com.<span style="color: #006633;">kodegeek</span>.<span style="color: #006633;">blog</span>.<span style="color: #006633;">twitter</span>.<span style="color: #006633;">TwitterPing</span> getAccessToken
INFO<span style="color: #339933;">:</span> Token<span style="color: #339933;">=</span>Ujq2pzpG7PwZVAJmGHjWrDiVzFz6wfmxzx9r85y7s TokenSecret<span style="color: #339933;">=</span>hBHVuOWdJPBe5r3iVXklSJuwawssjqoBslLHXhzlE Pin<span style="color: #339933;">=</span><span style="color: #cc66cc;">4129706</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> in thread <span style="color: #0000ff;">&quot;AWT-EventQueue-0&quot;</span> java.<span style="color: #006633;">lang</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aillegalstateexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IllegalStateException</span></a><span style="color: #339933;">:</span> Neither user ID<span style="color: #339933;">/</span>password combination nor OAuth consumer key<span style="color: #339933;">/</span>secret combination supplied</pre></td></tr></table></div>

<p>Por ahora <a href="http://groups.google.com/group/twitter4j/t/4e06e53d846cda27">ando preguntándole al autor</a> como resolver este asunto. La gente de Java API de FaceBook tiene una solución a este problema, lo hacen creando algo llamado &#8216;<a href="http://code.google.com/p/facebook-java-api/wiki/DesktopMode">temporary secret</a>&#8216;</p>
<p>Actualización: El siguiente &#8220;hack&#8221; parece funcionar, pues el código de HttpClient revizar por NULL o cadenas de caracteres vacias:</p>

<div class="wp_codebox"><table><tr id="p20016"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p2001code6"><pre class="java" style="font-family:monospace;">		Twitter twitter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Twitter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		twitter.<span style="color: #006633;">setOAuthConsumer</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span>, <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		twitter.<span style="color: #006633;">setOAuthAccessToken</span><span style="color: #009900;">&#40;</span>accessToken<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://kodegeek.com/blog/2009/07/18/enviando-tweets-desde-java-usando-twitter4j-%c2%bfcomo-proteger-consumerkey-and-consumersecret/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Como enviar Tweets desde Java usando Twitter4J</title>
		<link>http://kodegeek.com/blog/2009/07/05/como-enviar-tweets-desde-java-usando-twitter4j/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=como-enviar-tweets-desde-java-usando-twitter4j</link>
		<comments>http://kodegeek.com/blog/2009/07/05/como-enviar-tweets-desde-java-usando-twitter4j/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 12:43:12 +0000</pubDate>
		<dc:creator>josevnz</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[kodegeek]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[programación]]></category>
		<category><![CDATA[stupidzombie]]></category>
		<category><![CDATA[swing]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[twitter4j]]></category>
		<category><![CDATA[twitterping]]></category>

		<guid isPermaLink="false">http://kodegeek.com/blog/?p=1940</guid>
		<description><![CDATA[Una de las cosas que quiero hacer para la versión 1.1 de StupidZombie es agregarle soporte para actualizar el estado de Twitter cada vez que hacemos un ping. Como siempre es el asunto de construir algo desde cero o utilizar una herramienta existente y en el caso de StupidZombie lo que quiero es implementar la [...]]]></description>
			<content:encoded><![CDATA[<p>Una de las cosas que quiero hacer para la versión 1.1 de StupidZombie es agregarle soporte para actualizar el estado de Twitter cada vez que hacemos un ping. Como siempre es el asunto de construir algo desde cero o utilizar una herramienta existente y en el caso de StupidZombie lo que quiero es implementar la funcionalidad lo más rápido posible (por ejemplo, no tengo tiempo para seguir los cambios en el<a href="http://apiwiki.twitter.com/FAQ"> API</a> de Twitter).</p>
<p>Después de <a href="http://apiwiki.twitter.com/Libraries#Javanbsp">buscar en la red</a> me conseguí que <a href="http://yusuke.homeip.net/twitter4j/en/index.html">Twitter4J </a>es quizas la versión más madura para Java (recuerden, StupidZombie está escrito en ese lenguaje). Ni corto ni perezoso me puse a echar código y al final me decidí escribir una pequeña aplicación en Swing la cual hace lo siguiente:</p>
<ol>
<li>Autoriza la aplicación contra su cuenta de Twitter</li>
<li>Obtiene un PIN y claves especiales de autorización (lo cual no es lo mismo que su usuario clave, a eso se le conoce como<a href="http://apiwiki.twitter.com/Authentication"> OAuth</a>).</li>
<li>Envia un mensaje (tweet) a su cuenta en Twitter desde la aplicación en Java</li>
</ol>
<p>Nota, si usted es el desarrollador de la aplicación entonces lo primero que hay que hacer <a href="http://twitter.com/oauth_clients/new">es registrar una aplicación nueva</a> en Twitter (sus usuarios pueden saltarse este paso). Una vez terminado se ve como lo siguiente:</p>
<p><a href="http://www.flickr.com/photos/josevnz/3689339643/" title="Registered applications by josevnz, on Flickr"><img src="http://farm4.static.flickr.com/3588/3689339643_c64de4f818.jpg" width="500" height="345" alt="Registered applications" /><br />
Aplicaciones registradas en Twitter</a></p>
<p>Una vez registrada debemos pasar &#8220;Consumer key&#8221; y &#8220;Consumer Secret&#8221; a nuestro código de Twitter4j, yo lo hago en el constructor:</p>

<div class="wp_codebox"><table><tr id="p194011"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p1940code11"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">private</span> TwitterPing<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> consumerKey, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> consumerSecret<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> TwitterException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;KodeGeek simple Twitter pinger&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>consumerKey <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aillegalargumentexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IllegalArgumentException</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Consumer key is missing&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>consumerSecret <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aillegalargumentexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IllegalArgumentException</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Consumer secret is missing&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		twitter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Twitter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		twitter.<span style="color: #006633;">setOAuthConsumer</span><span style="color: #009900;">&#40;</span>consumerKey, consumerSecret<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		requestToken <span style="color: #339933;">=</span> twitter.<span style="color: #006633;">getOAuthRequestToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		setPreferredSize<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adimension+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Dimension</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">600</span>, <span style="color: #cc66cc;">400</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Desde la aplicación en Swing hacemos clic en el botón que dice &#8220;Authorize KodeGeek on Tweeter&#8221;. Si el soporte de Java para escritorio está activado entonces el navegador por omisión se arrancará e iremos a Twitter:</p>
<p><a href="http://www.flickr.com/photos/josevnz/3689301415/" title="Allowing KodeGeek by josevnz, on Flickr"><img src="http://farm3.static.flickr.com/2501/3689301415_dd8242c8c1.jpg" width="500" height="233" alt="Allowing KodeGeek" /><br />
Hora de autorizar a KodeGeek <img src='http://kodegeek.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </a></p>
<p>El código que hace esto es super sencillo :</p>

<div class="wp_codebox"><table><tr id="p194012"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code" id="p1940code12"><pre class="java" style="font-family:monospace;">	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Send the user to the authorization webpage
	 * @throws Exception 
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> sendUserToAuthUrl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> TwitterPing instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
		log.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>Level.<span style="color: #006633;">INFO</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Got authorization URL: %s&quot;</span>, requestToken.<span style="color: #006633;">getAuthorizationURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Desktop.<span style="color: #006633;">isDesktopSupported</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			Desktop.<span style="color: #006633;">getDesktop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">browse</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URI<span style="color: #009900;">&#40;</span>requestToken.<span style="color: #006633;">getAuthorizationURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Redirection was successfull.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aeventqueue+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">EventQueue</span></a>.<span style="color: #006633;">invokeLater</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arunnable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Runnable</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
				@Override
				<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajoptionpane+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JOptionPane</span></a>.<span style="color: #006633;">showMessageDialog</span><span style="color: #009900;">&#40;</span>
							instance, 
							<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cannot call default browser, please go to this URL instead: %s&quot;</span>, requestToken.<span style="color: #006633;">getAuthorizationURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, 
							<span style="color: #0000ff;">&quot;Problems trying to send the user to default page&quot;</span>, 
							<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajoptionpane+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JOptionPane</span></a>.<span style="color: #006633;">ERROR_MESSAGE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Si la autorización es aceptada (como se ve a continuación):</p>
<p><a href="http://www.flickr.com/photos/josevnz/3690106764/" title="KodeGeek twitter application by josevnz, on Flickr"><img src="http://farm3.static.flickr.com/2655/3690106764_7da7b4e8b9.jpg" width="500" height="248" alt="KodeGeek twitter application" /><br />
La autorización trabajó</a></p>
<p>Entonces podemos pedir el token de acceso usando el PIN obtenido en el paso anterior:</p>

<div class="wp_codebox"><table><tr id="p194013"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p1940code13"><pre class="java" style="font-family:monospace;">	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Call this method only after the user has authorized the application
	 * @throws TwitterException 
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> getAccessToken<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> pin<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> TwitterException <span style="color: #009900;">&#123;</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Got token: %s, Got secret: %s&quot;</span>, requestToken.<span style="color: #006633;">getToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, requestToken.<span style="color: #006633;">getTokenSecret</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		accessToken <span style="color: #339933;">=</span> twitter.<span style="color: #006633;">getOAuthAccessToken</span><span style="color: #009900;">&#40;</span>requestToken, pin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>accessToken <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
			log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Got access token for user %s&quot;</span>, accessToken.<span style="color: #006633;">getScreenName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Lo cual se ve así:</p>
<p><a href="http://www.flickr.com/photos/josevnz/3689301437/" title="Ping number, required for desktop apps by josevnz, on Flickr"><img src="http://farm4.static.flickr.com/3596/3689301437_0ca4439ae5.jpg" width="500" height="211" alt="Ping number, required for desktop apps" />Número PIN para darle acceso a nuestro cliente a la cuenta de Twitter</a></p>
<p>Si todo va bien entonces podemos mostrar los 3 pedazos de la autorización necesarios para poder enviar un Tweet:</p>
<p><a href="http://www.flickr.com/photos/josevnz/3690106810/" title="After geting the auth tokens by josevnz, on Flickr"><img src="http://farm3.static.flickr.com/2450/3690106810_47832ab3b5.jpg" width="500" height="333" alt="After geting the auth tokens" /><br />
La aplicación tiene todo lo que necesita. Hora  de enviar un tweet</a></p>
<p>Ya hacer el tweet es trivial con el siguiente pedazo de código:</p>

<div class="wp_codebox"><table><tr id="p194014"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p1940code14"><pre class="java" style="font-family:monospace;">	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Send a tween using an existing AccessToken
	 * @param tweet The update to set
	 * @throws TwitterException If there is a problem updating the status
	 * @return The status of the tweet
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> Status sendTweet<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> tweet<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> TwitterException <span style="color: #009900;">&#123;</span>
		twitter.<span style="color: #006633;">setOAuthAccessToken</span><span style="color: #009900;">&#40;</span>accessToken<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> twitter.<span style="color: #006633;">updateStatus</span><span style="color: #009900;">&#40;</span>tweet<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Al final, ¡exito!:</p>
<p><a href="http://www.flickr.com/photos/josevnz/3689301475/" title="Success, tweet update by josevnz, on Flickr"><img src="http://farm3.static.flickr.com/2581/3689301475_855e82d83e.jpg" width="500" height="105" alt="Success, tweet update" /><br />
Como se ve nuestro mensaje en Twitter, enviado desde Java</a></p>
<p>El protocolo de Twitter soporta muchísimas cosas más. En particular le recomiendo que se lean la documentación relacionada para entender más como trabajan las cosas, en especial la autenticación usando OAth. Ahhh, y por supuesto <a href="http://elangelnegro.cvs.sourceforge.net/viewvc/elangelnegro/src/java/main/com/kodegeek/blog/twitter/">aquí les dejo el código fuente completo</a> para que se diviertan echando código.</p>
<p>Hasta la siguiente entrega, dentro de unas horas me sale ir a una parrillada <img src='http://kodegeek.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Veneblogs: <a href="http://www.veneblogs.com/etiquetas/twitter" rel="tag">twitter</a>, <a href="http://www.veneblogs.com/etiquetas/twitter4j" rel="tag">twitter4j</a>, <a href="http://www.veneblogs.com/etiquetas/swing" rel="tag">swing</a>, <a href="http://www.veneblogs.com/etiquetas/kodegeek" rel="tag">kodegeek</a>, <a href="http://www.veneblogs.com/etiquetas/stupidzombie" rel="tag">stupidzombie</a><br />
<br />Blogalaxia: <a href="http://www.blogalaxia.com/tags/twitter" rel="tag">twitter</a>, <a href="http://www.blogalaxia.com/tags/twitter4j" rel="tag">twitter4j</a>, <a href="http://www.blogalaxia.com/tags/swing" rel="tag">swing</a>, <a href="http://www.blogalaxia.com/tags/kodegeek" rel="tag">kodegeek</a>, <a href="http://www.blogalaxia.com/tags/stupidzombie" rel="tag">stupidzombie</a><br />
<br />To2Blogs: <a href="http://www.to2blogs.com/tag/twitter" rel="tag">twitter</a>, <a href="http://www.to2blogs.com/tag/twitter4j" rel="tag">twitter4j</a>, <a href="http://www.to2blogs.com/tag/swing" rel="tag">swing</a>, <a href="http://www.to2blogs.com/tag/kodegeek" rel="tag">kodegeek</a>, <a href="http://www.to2blogs.com/tag/stupidzombie" rel="tag">stupidzombie</a><br />
<br />Technorati: <a href="http://technorati.com/tag/twitter" rel="tag">twitter</a>, <a href="http://technorati.com/tag/twitter4j" rel="tag">twitter4j</a>, <a href="http://technorati.com/tag/swing" rel="tag">swing</a>, <a href="http://technorati.com/tag/kodegeek" rel="tag">kodegeek</a>, <a href="http://technorati.com/tag/stupidzombie" rel="tag">stupidzombie</a><br />
<br />Del.icio.us: <a href="http://del.icio.us/tag/twitter" rel="tag">twitter</a>, <a href="http://del.icio.us/tag/twitter4j" rel="tag">twitter4j</a>, <a href="http://del.icio.us/tag/swing" rel="tag">swing</a>, <a href="http://del.icio.us/tag/kodegeek" rel="tag">kodegeek</a>, <a href="http://del.icio.us/tag/stupidzombie" rel="tag">stupidzombie</a></p>
]]></content:encoded>
			<wfw:commentRss>http://kodegeek.com/blog/2009/07/05/como-enviar-tweets-desde-java-usando-twitter4j/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

