Skip to content

Instantly share code, notes, and snippets.

@averyaube
Created May 26, 2011 18:32
Show Gist options
  • Select an option

  • Save averyaube/993724 to your computer and use it in GitHub Desktop.

Select an option

Save averyaube/993724 to your computer and use it in GitHub Desktop.
Example of twitter authentication in Kohana using Tijs Verkoyen's Twitter SDK
<?php
class Controller_Auth extends Controller {
public function action_twitter()
{
$twitter = new Twitter(CONSUMER_TOKEN, CONSUMER_SECRET);
// Retrieve Request Token
$twitter->oAuthRequestToken(CALLBACK_URL); // In this case, should go back to auth/twitter
if ( ! isset($_GET['oauth_token']))
{
$twitter->oAuthAuthorize();
}
else
{
// Retrieves Access Token
$token = $twitter->oAuthAccessToken($_GET['oauth_token'], $_GET['oauth_verifier']);
if (empty($token) || sizeof($token) < 4)
{
// Shouldn't happen, but I've experienced it before
die('Sorry, try again');
}
// You get the oauth_token, oauth_token_secret, user_id and screen_name variables.
// At this point, do whatever you are going to do (eg. see if a user exists with credentials and log them in, or register new user)
// Since this is likely to be in a popup, you can just hit a callback for the opener
$this->response->body('<script>window.opener.authCallback();</script>');
}
}
}
<a href='#" id='twitter_auth'>AUTHENTICATE!</a>
<script>
var authWindow;
function authCallback() {
authWindow.close();
}
$('#twitter_auth').click(function(e) {
e.preventDefault();
authWindow = window.open('auth/twitter', 'auth', 'width=500,height=400');
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment