Twitter api 1.1 php oauth using abrahams library problem

php

#1

i am using abrahams php library for twitter api v 1.1.
my code:

(index.php)

/LOADING LIBRARY

require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

//TWITTER APP KEYS
$consumer_key = 'MY CONSUMER KEY';
$consumer_secret = 'MY CONSUMER SECRET';

//CONNECTION TO THE TWITTER APP TO ASK FOR A REQUEST TOKEN
$connection = new TwitterOAuth($consumer_key, $consumer_secret);
$request_token = $connection->oauth("oauth/request_token", array("oauth_callback" => "callbackurl"));


//callback is set to where the rest of the script is

//TAKING THE OAUTH TOKEN AND THE TOKEN SECRET AND PUTTING THEM IN COOKIES (NEEDED IN THE NEXT SCRIPT)
$oauth_token=$request_token['oauth_token'];
$token_secret=$request_token['oauth_token_secret'];
setcookie("token_secret", " ", time()-3600);
setcookie("token_secret", $token_secret, time()+60*10);
setcookie("oauth_token", " ", time()-3600);
setcookie("oauth_token", $oauth_token, time()+60*10);

//GETTING THE URL FOR ASKING TWITTER TO AUTHORIZE THE APP WITH THE OAUTH TOKEN
$url = $connection->url("oauth/authorize", array("oauth_token" => $oauth_token));

//REDIRECTING TO THE URL
header('Location: ' . $url);

callbackurl:

/**
 * users gets redirected here from twitter (if user allowed you app)
 * you can specify this url in https://dev.twitter.com/ and in the previous script
 */ 

//LOADING LIBRARY
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

//TWITTER APP KEYS
$consumer_key = 'MY CONSUMER KEY';
$consumer_secret = 'MY CONSUMER SECRET';

//GETTING ALL THE TOKEN NEEDED
$oauth_verifier = $_GET['oauth_verifier'];
$token_secret = $_COOKIE['token_secret'];
$oauth_token = $_COOKIE['oauth_token'];

//EXCHANGING THE TOKENS FOR OAUTH TOKEN AND TOKEN SECRET
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $token_secret);
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $oauth_verifier));
$accessToken=$access_token['oauth_token'];
$secretToken=$access_token['oauth_token_secret'];

//DISPLAY THE TOKENS
echo "<b>Access Token : </b>".$accessToken."<br />";
echo "<b>Secret Token : </b>".$secretToken."<br />";

$statues = $connection->get("statuses/home_timeline", array("count" => 25, "exclude_replies" => true));
echo '<pre>';
 var_dump($statues);
echo '</pre>';

when i run index.php i get redirected to twitters authorization page.
when i authorize the app the access token and secret token get printed
on the screen yet i get an error: “Invalid or expired token”

i have searched everywhere and i dont understand what i did wrong…

plaease help!


#2

I don’t see any obvious issues but keep in mind that oauth/access_token can only be called once each time the user authorizes on twitter.com. If you just refresh the page and that tries to run a second time it will error out.


#3

hey
thank you for the quick response :slight_smile:
…can you help me out here?
this all oauth process is very complicated for me…
i looked every where on the web for an example on how to do this or a sample code but i found nothing!!!
the documentation on twitter developers isn’t much help cause ther is no good examples…
can you help me? can you give me a sample code that i can use to authenticate the user and start making queries via the twitter’s api?

thanx…


#4

https://twitteroauth.com is an interactive demo of the entire flow and the source code is available https://github.com/abraham/twitteroauth-demo