Kohana 3 OAuth & Twitter Demo Code

October 6, 2011

The Internet seems a bit sparse when looking for good demo code for using the Kohana 3 OAuth module with Twitter.

I think the main issue is that the OAuth module isn’t very well documented, and doesn’t do API requests. For that you need an API implementation, like shadowhand/apis.

Anyway, here is a gist I put together with an example controller for Twitter OAuth:

https://gist.github.com/1267793

Categories: Geek
Tags: , , , , ,

Comments

  1. Erik says:

    Hi John,

    Thanks for the example code.

    I have one issue I can’t figure out how to fix.
    When my code gets to

    Request::current()->redirect( $provider->authorize_url( $request_token ) );

    I get redirected to Twitter and get a 404 for a non existing page.

    I can’t figure out what it is that I’m doing wrong. Hope you can help me.

    Greetings,
    Erik

  2. Erik says:

    Hi John,

    Got a bit further in pinpointing the problem. $provider->authorize_url( $request_token ) will resolve into a URL and guide me to a page on twitter.com to login, but when I login I get the 404 on twitter.com

    The URL generated is something like:
    https://api.twitter.com/oauth/authenticate?oauth_token=l2Ss7wUsw5I69ySoQMazWe1CkndlnbKlzGkKDbo43c

    I get the Login button and after that I’m redirected to:
    https://api.twitter.com/twitter/callback?oauth_token=l2Ss7wUsw5I69ySoQMazWe1CkndlnbKlzGkKDbo43c&oauth_verifier=jIkhFqzUvKpPt8QNUAzPSjPydnx3x9ClkvHVB4rmZU

    which, according to twitter.com, does not exist.

  3. John Hobbs says:

    Hey Erik!

    I believe what’s happening is that you do not have a domain specified in your application.

    So in action_index when you set the callback, $consumer->callback( URL::site(... that is returning a base relative URL, i.e. /twitter/callback.

    Twitter is taking that callback and passing it straight to the browser after you authenticate, which the browser interprets as being relative to api.twitter.com – thus the 404.

    You can either set your base_url to include your domain, or tweak the $consumer->callback to provide it with an absolute URL.

  4. Erik says:

    Hi John,

    Thanks. That was indeed the problem.
    I placed the URL in the code for now. I did try to put it in the config file, but that didn’t work.
    Might have been the wrong config file (/modules/oauth/config/oauth.php)?

    Anyway: Thanks a lot!

Leave A Comment

Your email will not be published.