Sunday, March 28, 2010

Twitter OAuth Login - Part 3

1. Part 1
2. Part 2

Note : As mentioned in part 2 , we will be using Elliot Haughin's twitter library for this article. Please note that library uses codeigniter framework.

So the actual coding integration is actually consist of totally three functions or actions.

1. Create a Twitter login Button.

All you need to do is this,

<a href="index.php/login/twitterLogin" >
<img style="border: 0px none ;" src="images/connenct-twitter.jpg" />

2. Create a function to handle Twitter button click

In our above code, that function or page is, 'index.php/login/twitterLogin'. So when the user will click the link, this function will be called.

This function will redirect the user to Twitter along with sending the consumer key and consumer secret key information. Now from where to get these keys ? If you remember in the last part we registered an application with Twitter, so when Twitter registers an application, it assigns the application a consumer and consumer secret . You can get this information from the application page.


Our function will look like this

function twitterLogin()
{
try
{

$consumer_key = $this->config->item('twitter_consumer_key');
$consumer_key_secret = $this->config->item('twitter_consumer_secret');

$this->load->library('twitter');
$this->twitter->oauth($consumer_key,$consumer_key_secret);
}
catch(Exception $ex)
{
log_message('error', $ex->getMessage());
$error = 'Unable to connect to Twitter, please try after a while.';
}
}
That's all! User will be redirected to Twitter where he will be asked to authorize your application. Once the user allows or denies the access, he will be redirected back to your website. This url will be the 'callback' url you provided at the time of application registration.

3. Create a function to handle User redirection from Twitter

In this function, you will know if the user allowed the access or denied. If allowed then you will talk back to Twitter to send you access token for user. You may want to store this access token in database in order to to use it for future actions like updating his twitter status from your application.

function twitterCallBack()
{
try
{

if(isset($_GET['denied']))
{
header('Location: ' . $this->config->item('base_url'));
return;
}

$consumer_key = $this->config->item('twitter_consumer_key');
$consumer_key_secret = $this->config->item('twitter_consumer_secret');

$this->load->library('session');

$tokens['access_token'] = NULL;
$tokens['access_token_secret'] = NULL;

// GET THE ACCESS TOKENS

$oauth_tokens = $this->session->userdata('twitter_oauth_tokens');

if ( $oauth_tokens !== FALSE ) $tokens = $oauth_tokens;

$this->load->library('twitter');

$auth = $this->twitter->oauth($consumer_key, $consumer_key_secret, $tokens['access_token'], $tokens['access_token_secret']);

if ( isset($auth['access_token']) && isset($auth['access_token_secret']) )
{
// SAVE THE ACCESS TOKENS

$this->session->set_userdata('twitter_oauth_tokens', $auth);

if ( isset($_GET['oauth_token']) )
{
$uri = $_SERVER['REQUEST_URI'];
$parts = explode('?', $uri);

// Now we redirect the user since we've saved their stuff!

header('Location: '.$parts[0]);
return;
}
}

header('Location: ' . $this->config->item('base_url').'index.php/dashboard');
}
catch(Exception $ex)
{
log_message('error', $ex->getMessage());

}

}
...And you are done!

1 comment: