Twitter API 的无效/过期令牌错误
我正在使用 Jason Mathai 的 PHP OAUTH 库,每当我尝试使用以下测试代码访问用户信息时,我都会收到无效/过期令牌错误:
//sessions stuff here
session_start();
//twitter stuff
include 'lib/EpiCurl.php';
include 'lib/EpiOAuth.php';
include 'lib/EpiTwitter.php';
include 'lib/secret.php';
//ensure token is set for the session
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
if($_GET['oauth_token']){
$_SESSION['o_token'] = $_GET['oauth_token'];
}
if(!isset($_SESSION['o_token']))
{
$url = $twitterObj->getAuthorizationUrl();
header("Location:$url");
die();
}
$o_token = $_SESSION['o_token'];
$twitterObj->setToken($o_token);
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
setcookie('oauth_token', $token->oauth_token);
setcookie('oauth_token_secret', $token->oauth_token_secret);
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret,$_COOKIE['oauth_token'], $_COOKIE['oauth_token_secret']);
$twitterInfo= $twitterObj->get_accountVerify_credentials();
$twitterInfo->response;
var_dump($twitterInfo->response); // this tells me the error
try{
echo $twitterInfo->screen_name;
}catch(EpiTwitterException $e){
echo $e->getMessage();
}
所有方法都直接映射到使用 twitter API 的 HTTP get 调用 我假设我的问题与 Jason 的库无关(因为它使用得相当好),但与我的逻辑有关。感谢所有帮助!
I am using Jason Mathai's PHP OAUTH library and i keep on getting an invalid/expired token error whenever i try to access a user's information with the following test code:
//sessions stuff here
session_start();
//twitter stuff
include 'lib/EpiCurl.php';
include 'lib/EpiOAuth.php';
include 'lib/EpiTwitter.php';
include 'lib/secret.php';
//ensure token is set for the session
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
if($_GET['oauth_token']){
$_SESSION['o_token'] = $_GET['oauth_token'];
}
if(!isset($_SESSION['o_token']))
{
$url = $twitterObj->getAuthorizationUrl();
header("Location:$url");
die();
}
$o_token = $_SESSION['o_token'];
$twitterObj->setToken($o_token);
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
setcookie('oauth_token', $token->oauth_token);
setcookie('oauth_token_secret', $token->oauth_token_secret);
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret,$_COOKIE['oauth_token'], $_COOKIE['oauth_token_secret']);
$twitterInfo= $twitterObj->get_accountVerify_credentials();
$twitterInfo->response;
var_dump($twitterInfo->response); // this tells me the error
try{
echo $twitterInfo->screen_name;
}catch(EpiTwitterException $e){
echo $e->getMessage();
}
All the methods map directly to HTTP get calls with the twitter API I assume my problem has nothing to do with Jason's library (since it is fairly well used) but something to do with my logic. All help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦您从 Twitter 获得了 oauth_token,您就可以将其存储到会话中,并且每次您使用同一存储的会话发出请求时。令牌在服务器上设置了一些过期时间,因此一段时间后,您开始收到过期令牌错误。
此链接可以让您了解如何创建简单的 Twitter 应用程序。
Once you get a oauth_token from twitter, you are storing it into session and every time you make a request with that same stored session. Tokens have some expiry time set on server so after some time, you start getting expiry token error.
This link can give you some understanding for creating a simple twitter app.