Skip to content

Instantly share code, notes, and snippets.

@jmhobbs
Created October 6, 2011 16:03
Show Gist options
  • Save jmhobbs/1267793 to your computer and use it in GitHub Desktop.
Save jmhobbs/1267793 to your computer and use it in GitHub Desktop.
Kohana 3 - OAuth & Twitter Example
<?php
class Controller_Twitter extends Controller {
public function action_index () {
// Load a consumer from config file.
$consumer = OAuth_Consumer::factory( Kohana::config( 'oauth.twitter' ) );
// Check if we already have an access token in the session.
$access_token = Session::instance()->get( 'oauth_access_token', null );
if( is_null( $access_token ) ) {
// We need to set the callback to action_callback. This can also be done in the config file.
$consumer->callback( URL::site( Route::get( 'default' )->uri( array( 'controller' => 'twitter', 'action' => 'callback' ) ) ) );
// The provider actually makes the requests
$provider = OAuth_Provider::factory( 'twitter' );
$request_token = $provider->request_token( $consumer );
Session::instance()->set( 'oauth_token', $request_token );
// Once we have the request token, we redirect to Twitter to confirm.
Request::current()->redirect( $provider->authorize_url( $request_token ) );
}
else {
// Now that we have a valid access token, we can make authorized API requests
$t = Twitter::factory( 'account' );
$this->response->body( var_export( $t->verify_credentials( $consumer, $access_token ) );
}
}
public function action_callback () {
// Pull our request token out of the session. We use get_once because request tokens are single use
$request_token = Session::instance()->get_once( 'oauth_token' );
// Add the verifier from the query parameters
$request_token->verifier( Request::current()->query( 'oauth_verifier' ) );
// Rebuild the consumer and provider
$consumer = OAuth_Consumer::factory( Kohana::config( 'oauth.twitter' ) );
$provider = OAuth_Provider::factory( 'twitter' );
// Exchange the request token for a longer term access token
$access_token = $provider->access_token( $consumer, $request_token );
Session::instance()->set( 'oauth_access_token', $access_token );
Request::current()->redirect( Route::get( 'default' )->uri( array( 'controller' => 'twitter' ) ) );
}
}
/*
Example output of action_index once it is Twitter authorized:
object(stdClass)#55 (39) {
["id_str"]=>
string(7) "6116372"
["profile_link_color"]=>
string(6) "0ae000"
["protected"]=>
bool(false)
["url"]=>
string(27) "http://www.velvetcache.org/"
["profile_image_url_https"]=>
string(64) "https://si0.twimg.com/profile_images/732683067/avatar_normal.png"
["screen_name"]=>
string(7) "jmhobbs"
["profile_image_url"]=>
string(62) "http://a2.twimg.com/profile_images/732683067/avatar_normal.png"
["name"]=>
string(10) "John Hobbs"
["listed_count"]=>
int(21)
["profile_background_color"]=>
string(6) "ffffff"
["lang"]=>
string(2) "en"
["profile_background_tile"]=>
bool(true)
["utc_offset"]=>
int(-21600)
["description"]=>
string(85) "Bit Conservationist.
I Love Omaha. I Love Code. I make fancy websites @whatcheer."
["show_all_inline_media"]=>
bool(false)
["contributors_enabled"]=>
bool(false)
["created_at"]=>
string(30) "Thu May 17 18:38:22 +0000 2007"
["profile_sidebar_fill_color"]=>
string(6) "e6e6e6"
["default_profile"]=>
bool(false)
["statuses_count"]=>
int(1187)
["followers_count"]=>
int(247)
["status"]=>
object(stdClass)#56 (19) {
["id_str"]=>
string(18) "121822869910077441"
["retweet_count"]=>
int(0)
["in_reply_to_screen_name"]=>
NULL
["in_reply_to_user_id"]=>
NULL
["truncated"]=>
bool(false)
["retweeted"]=>
bool(false)
["possibly_sensitive"]=>
bool(false)
["in_reply_to_status_id_str"]=>
NULL
["geo"]=>
NULL
["place"]=>
NULL
["coordinates"]=>
NULL
["created_at"]=>
string(30) "Thu Oct 06 05:43:30 +0000 2011"
["in_reply_to_user_id_str"]=>
NULL
["contributors"]=>
NULL
["source"]=>
string(3) "web"
["in_reply_to_status_id"]=>
NULL
["favorited"]=>
bool(false)
["id"]=>
int(121822869910077441)
["text"]=>
string(106) "Steve Jobs died today. So did 4,000 children from water related diseases. #justsayin http://t.co/XCJJnUaJ"
}
["default_profile_image"]=>
bool(false)
["time_zone"]=>
string(26) "Central Time (US & Canada)"
["favourites_count"]=>
int(286)
["profile_sidebar_border_color"]=>
string(6) "000000"
["is_translator"]=>
bool(false)
["following"]=>
NULL
["geo_enabled"]=>
bool(false)
["notifications"]=>
NULL
["profile_use_background_image"]=>
bool(true)
["profile_background_image_url_https"]=>
string(72) "https://si0.twimg.com/profile_background_images/330137786/1-SEAMLESS.jpg"
["follow_request_sent"]=>
NULL
["verified"]=>
bool(false)
["friends_count"]=>
int(194)
["profile_text_color"]=>
string(6) "000000"
["location"]=>
string(9) "Omaha, NE"
["id"]=>
int(6116372)
["profile_background_image_url"]=>
string(70) "http://a2.twimg.com/profile_background_images/330137786/1-SEAMLESS.jpg"
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment