Wednesday, April 30, 2014

PHP Twitter OAuth Login Dialog Implementation

This simple code is used for authentication in twitter. It is an OAuth Login using PHP SDK Codebird Adapter.

Get Your Twitter Developer Account ( Consumer Key & Consumer Secret )
https://dev.twitter.com/

Source Code
<?php session_start();

require_once 'codebird.php';

define('CONSUMER_KEY', '<YOUR-CONSUMER-KEY>');
define('COMSUMER_SECRET', '<YOUR-CONSUMER-SECRET>');

Codebird::setConsumerKey(CONSUMER_KEY, COMSUMER_SECRET);
$tweet = Codebird::getInstance();

if (!isset($_REQUEST['oauth_verifier'])) {

        $response = $tweet->oauth_requestToken(array('oauth_callback' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']));
        $tweet->setToken($response->oauth_token, $response->oauth_token_secret);

        $_SESSION['OAUTH_TOKEN']        = $response->oauth_token;
        $_SESSION['OAUTH_TOKEN_SECRET'] = $response->oauth_token_secret;

        $auth_url = $tweet->oauth_authorize();
        header('Location: ' . $auth_url);
        exit;
    
    } else {
    
        $tweet->setToken($_SESSION['OAUTH_TOKEN'], $_SESSION['OAUTH_TOKEN_SECRET']);
        $response = $twitt->oauth_accessToken(array('oauth_verifier' => $_REQUEST['oauth_verifier']));

        $_SESSION['OAUTH_TOKEN']        = $response->oauth_token;
        $_SESSION['OAUTH_TOKEN_SECRET'] = $response->oauth_token_secret;
}

/* Get your account details.*/

$tweet->setReturnFormat(CODEBIRD_RETURNFORMAT_ARRAY);
$acct_info = $tweet->account_verifyCredentials();
var_dump($acct_info);




No comments:

Post a Comment

Genesis Framework for WordPress