require 'twitteroauth/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
session_start();
try{
$config = require_once 'config.php';
// get and filter oauth verifier
$oauth_verifier = filter_input(INPUT_GET, 'oauth_verifier');
// check tokens
if (empty($oauth_verifier) || empty($_SESSION['oauth_token']) || empty($_SESSION['oauth_token_secret'])) {
// something's missing, go and login again
//header('Location: ' . $config['url_login']);
}
// connect with application token
$connection = new TwitterOAuth(
$config['consumer_key'],
$config['consumer_secret'],
$_SESSION['oauth_token'],
$_SESSION['oauth_token_secret']
);
//set cookies to allow connection back to twitter api
setcookie('oauth_verifier', $oauth_verifier);
setcookie('consumer_key', $config['consumer_key']);
setcookie('consumer_secret', $config['consumer_secret']);
setcookie('oauth_token', $_SESSION['oauth_token']);
setcookie('oauth_token_secret', $_SESSION['oauth_token_secret']);
// request user token
$token = $connection->oauth(
'oauth/access_token', [
'oauth_verifier' => $oauth_verifier
]
);
// connect with user token
$twitter = new TwitterOAuth(
$config['consumer_key'],
$config['consumer_secret'],
$token['oauth_token'],
$token['oauth_token_secret']
);
$user = $twitter->get('account/verify_credentials');
$toReturn = 'Logged in as ' . $user->name . ' - @' . $user->screen_name . ' |Log out';
$msgToShow = "'Type your rant here'";
}catch (Exception $e) {
$toReturn = 'Log In';
$msgToShow = "'Please log in before typing' disabled";
}
?>
thread