Posts Tagged ‘Twitter4j’
Stalling with tweeting from Hardwire
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.
There’s no simple way to wire Twitter4j in the Spring Framework
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.
Twitter4j Oauth + Spring: Class Twitter across multiple requests
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 »
New Problems with Twitter4j Oauth
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)
MethodInvokingBean: Wiring with two arguments in Spring
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>