Research Log of Web Science Students

Computer Science is not simply programming

Posts Tagged ‘Twitter4j

Stalling with tweeting from Hardwire

leave a comment »

I can’t believe I am back to bloggin about Twitter4J. This time I am trying to tweet from Twitter4J. Before I only needed to get the Twitter profile.

This code doesn’t seem to work:

twitter.setOAuthConsumer(consumerKey,consumerSecret);
twitter.setOAuthAccessToken(new AccessToken(token,tokenSecret));
try {
	twitter.updateStatus(tweet);
} catch (TwitterException e) {
	throw new OAuthClientException(e.getMessage());
      }

The error I get is

/statuses/update.xml
Read-only application cannot POST

UPDATE: But I already did this before. Maybe I have to get a new user again.

Advertisements

Written by Jose Asuncion

October 9, 2009 at 8:48 pm

Posted in Hardwire

Tagged with ,

There’s no simple way to wire Twitter4j in the Spring Framework

with one comment

jeune.writingCodeException: 404 SEVERE REFACTORING FAIL 
                                      Jeune, you're a noob!

I have been attempting to wire Twitter4j in Spring for the past two days now. Although we’ve been successful at accessing Twitter data using Oauth already, I wasn’t satisfied at just making the code work. What we came up with was a “code duplication” monster and the only way to deal with this thing is through shotgun surgery.

I tried 6 sets of wirings for 2 code designs over 24 hours where I only ate twice (a small serving of pasta with pesto sauce). I was all over the SpringSource,  StackOverFlow and Twitter4j forums asking for help. Although some did come none weren’t enough to solve my problem.

What did I realize?

It’s time to move on.

At the end of day as researchers we will be judged on the features we’ve implemented and not on the refactorings we’ve done. I hope to keep that in mind.

My recommendations: Of course there’s way to wire this. I am thinking about extending AbstractController by an AbstractMethod, TwitterController and then put the relevant code in a protected method so I the controllers who need access to twitter can instantiate and initialize a Twitter class.

That would mean me reading chapter 3 of Spring in Action.

Hmmm I am already thinking about other the consequences of doing this. What if we’re going to access other social networks? Should I make a FacebookController too? I like that challenge, this is where OO design patterns come into play.

Anyway for my travails on trying to wire Twitter4j, here you go.

Written by Jose Asuncion

September 6, 2009 at 8:24 am

Posted in Hardwire

Tagged with ,

Twitter4j Oauth + Spring: Class Twitter across multiple requests

leave a comment »

The way I use the Twitter class in Twitter4j, I have to instantiate a new Twitter class for every request I receive.

For example in a Spring Controller,

Twitter twitter = new Twitter();

that’s very trivial to wire but my problem is what happens next:

Twitter twitter = new Twitter();
twitter.setOauthConsumer("consumerKey",consumerSecret");

How on earth will I wire something like that in Spring? Read the rest of this entry »

Written by Jose Asuncion

September 5, 2009 at 7:21 pm

New Problems with Twitter4j Oauth

leave a comment »

The problem: I can’t seem to get my profile image after authorization even if the logs show that I can get the url of my profile image!

What’s even more confusing is that I can get my screename and description, print them to the jsp but can’t do this:

<img src="${page['profileImage']}">

but in my servlet where I try to get the url of the image, the description and screename everything is printed in the logs just okay.

I am getting this twitter4j exception in my jsp

twitter4j.TwitterException: The user has not given access to the account.
	at twitter4j.http.HttpClient.getOAuthAccessToken(HttpClient.java:221)
	at twitter4j.Twitter.getOAuthAccessToken(Twitter.java:179)
	at com.hardwire.controller.AuthController.handleRequestInternal(AuthController.java:45)
	at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)

Read the rest of this entry »

Written by Jose Asuncion

September 5, 2009 at 5:32 pm

MethodInvokingBean: Wiring with two arguments in Spring

leave a comment »

Another case of duplicating code in my app:

Twitter twitter = new Twitter();
twitter.setOAuthConsumer("consumerKey","consumerSecret");

The first solution I thought of was eliminate the duplicate code using dependency injection. Only that I thought you can only wire properties
one by one like for example:

<bean  id="MyPageController"
class="com.hardwire.controller.MyPageController">
 <property name="twitterService" ref="twitterService"/>
 <property name="userService" ref="googleUserService"/>
</bean>

But with Spring’s MethodInvokingBean, this can be instantiated and injected to the objects who need it.

<bean id="twitter" class="twitter4j.Twitter"></bean>

<bean id="twitterInjector" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="twitter"/>
<property name="targetMethod">
              <value>setOAuthConsumer</value>
</property>
<property name="arguments">
     <list>
       	<value>consumerKey</value>
       	<value>consumerSecret</value>
     </list>
 </property>
 </bean>    

Written by Jose Asuncion

September 3, 2009 at 6:06 pm

My Quick and Dirty Twitter4j OAuth for Web Apps

with 2 comments

This simple tutorial is a result of my trying to oauth to twitter using Twitter4j. This is a small functionality in my thesis. Anyway, I have been googling tutorials on how to use Twitter4j in a web app but I haven’t found much. The Twitter4j has examples but most of them in are written in a main method. I did find some a lead in the Twitter4j google group and much to my surprise, the key to accessing data in Twitter using Twitter4j/Oauth is knowing how Oauth works in the first place.

This tutorial isn’t about how to use the oauth protocol there are many resources in the net about that .

Try here, here and well, here.

Although this code works, it might not be the right way to use Twitter4j and Oauth. It might not be optimized and it’s definitely not the best way. It’s actually riddled with bad practices! Although in my real app, I have refactored those code smells already.

Another thing, I have yet to address why Twitter’s icon shows up after it redirects to the to callback url. I intend to work on that issues soon as well as optimizing the method. Comments and suggestions are highly welcome!

http://jeungun.wordpress.com/2009/09/03/quick-and-dirty-twitter4j-oauth-for-web-apps/

Written by Jose Asuncion

September 3, 2009 at 5:26 pm

Finding a Web Solution for Twitter4j: Results of Session Solution

with 3 comments

(INTRO: I can’t seem to figure out how to use Twitter4j Oauth in a web app. The code in the website only gives examples in a main method.

Googling Twitter4j Tutorial Web and other related searches returns nil useful results. Through this series of blogposts I HOPE to be able address this dearth.

My Problem: I’d like to be able to access the RequestToken and Twitter instantiations after authorizing from Twitter)

In my previous post I tried to store the RequestToken and Twitter instantiations to the Appengine’s datastore so I can use it in the callback url after the user authorises Twitter. But apparently the appengine datastore doesn’t support the RequestToken Class.

My next solution: put both instantiations in the session so I can access it in when the user is redirected to the callback url or from my(the developer’s) pov, so that I can access it in the controller class (no not the servlet I am using Spring btw).

So far I’ve been able to load the page and that means I was able to store it in the session, log returns no errors! I have no idea right now how to test views in Spring, I hope I did so the debugging I am doing here will rely more on logging.
sessionTests

Next step is to set the callback url to a controller to access it from there. Watch this space!

Update: Unit tested storing an access token to see if the datastore accepts it.

Well the test passed but Read the rest of this entry »

Written by Jose Asuncion

August 24, 2009 at 4:53 pm