Sep 23, 2010

Twitter, ColdFusion ( Railo ), Java and Oauth.

Twitter, ColdFusion and Oauth.

Got some sort of Twitter botthat posts to Twitter and found its no longer working? happened to me and it's due to Twitter no longer supporting the basic authentication methods.

So that meant I had to use oAuth. Hmmm that could be fun and actually it was :)

The server was actually running Railo so I have access to cfml and java. I hopped over to: http://twitter4j.org/en/index.html

Downloaded the jar required. SSH to the server and drop the file in:

/var/www/virtual/www.webapp.com/WEB-INF/railo/lib/

folder, logged in to the Server admin and restarted Railo.

I now had access to the jars ( and only for the one app not server wide like ACF ).

Run this bit of code:



And I was up and running.

But first you do need to setup and register your app with Twitter. Go here: http://dev.twitter.com and register the app.

There you will get your key and secret key.

The next step is the access key. This is provided but needs a few extra clicks.

On the right you will see "My Access Token" click this and you can retrieve the access token and the access token secret.

17 comments:

  1. Any reason you opted for that rather than a ColdFusion solution like http://blog.pcsilva.com/index.cfm/CFTwitterLib?

    ReplyDelete
  2. Yes. That does not support oAuth which is required as I mentioned. I already had my own library built which stopped working last month.

    ReplyDelete
  3. Could of saved yourself some work and used Matt Giffords! http://monkehtweet.riaforge.org/

    ReplyDelete
  4. Nice hadn't seen that one but would not have saved me any time in this instance.

    ReplyDelete
  5. What fortuitous timing; I've been wanting to play around with Twitter oAuth as well (I have an idea for a little hackathon project). When you get this stuff in place, do you also have to do the thing where the user gets a pop-up and "Allows your app" to update your Twitter info?

    ReplyDelete
  6. Hmm, last comment didn't let me subscribe - trying a different "Comment As". Sorry for the spam.

    ReplyDelete
  7. I built a cf Twitter irc type bot to listen for commands using monkehtweets last weekend. 3.5 hours total.

    Could probably do it in an hour now. The oAuth code in monkeh worked flawlessly, even let's you connect to multiple accounts or hardwire to one. He could probably swing the oAuth code into a CFC of it's own :)

    ReplyDelete
  8. Hey Paul

    Great work, and thank you to Jon and Jas for mentioning monkehTweets.

    Either option will work seamlessly for developers, although twitter4J needs to have the jAR files placed and referenced on the server, which isn't possible for any developers using a shared hosting environment.

    ReplyDelete
  9. Thanks for the comments. If I had have come across monkehtweets first I'm sure I would have used it. I'm lucky I have my own VPS so dropping in a jar is fine.

    @Ben yes it will do the "jump to twitter page to sigh in" part of oAuth. It works well.

    ReplyDelete
  10. I played around with oAuth this weekend... and by played around, I mean I spent 8 hours trying to get my signature to match up :) I FINALLY got something simple working (login in and direct message).

    @Matt, I took a look at MonkehTweets - looks super robust. I kept referring back to it when I couldn't get my test scripts to run. Thanks!

    ReplyDelete
  11. Far too difficult getting Railo working, no clear documentation.

    Its just not viable at this time for a real business compared to Adobe Coldfusion.

    ReplyDelete
  12. @Anonymous if you are a simple developer then perhaps. I prefer to take a different approach and fully understand what my application server is doing. If you struggle with an install of Railo then its likely that your SCF install is running totally standard and insecure. Take the time to learn and you'll find all sorts of gains. Best of luck.

    ReplyDelete
  13. @Anonymous Also consider donating to the documentation fund via a Pay Pal donation here: paypal@getrailo.com

    ReplyDelete
  14. I am using railo
    i download the twitter4j-android-2.2.3 and put the /lib/ folder jar files in railo /lib/ directory and restart the railo.
    using the code

    < cfset Twitter = createObject("java", "twitter4j.Twitter") >
    < cfset Twitter.setOAuthConsumer("Consumer key","Consumer secret") >

    Getting error "No matching Constructor for twitter4j.Twitter() found"
    Please help me to slve this problem?

    ReplyDelete
  15. @Jibu

    There has been a new release of Twitter4j and it's no nonger the same. I can provide a link to the jar that these code examples work with or you'll have to go back to the docs at twitter4j and work out the new syntax. Let me know if you want the jar.

    ReplyDelete
  16. As an update I went back and started over and used MonkehTweets and it's great.

    ReplyDelete