Twitter OAuth:无效或过期的令牌[不重复] [英] Twitter OAuth : Invalid or expired token [its NOT duplicate]

查看:88
本文介绍了Twitter OAuth:无效或过期的令牌[不重复]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在有人匆忙将这个问题标记为重复之前,让我告诉你它不是重复的

Before anyone goes in a hurry and mark this question as Duplicate, let me tell you that ITS NOT DUPLICATE

我已经检查过类似的问题,例如this这个this 和 this,但这些都是 2 岁,从那时起图书馆已经改变太多,所以这些答案不是有用.

I have already checked similar question like this, this, this and this, but those are all 2 years old and the library has been changed too much since then so those answers are not useful.

那么问题来了.我正在使用 abraham 的图书馆,可以在 此处 找到.以下是我正在使用的代码:

So here's the question. I'm using abraham's libraray which can be found here. Below is the code that I'm using:

if(!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret']))
{
    $connection = new TwitterOAuth('MY_CONSUMER_KEY', 'MY_CONSUMER_SECRET', $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

    $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
    $_SESSION['access_token'] = $access_token;

    $user_info = $connection->get("account/verify_credentials");
    print_r($user_info);
}

从我上面做的 print_r 得到的结果如下:

From the print_r which I did above, I get the result as follows:

stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [code] => 89 [message] => Invalid or expired token. ) ) )

由于此无效/过期令牌,我无法继续工作.所以我退后了一步:

Due to this invalid/expired token I'm not able to get ahead in my work. So I went 1 step back and did:

var_dump($access_token);

得到的结果是:

array(5) { 
   ["oauth_token"]=> string(50) "*********" 
   ["oauth_token_secret"]=> string(45) "*********" 
   ["user_id"]=> string(10) "***My user id****" 
   ["screen_name"]=> string(9) "***My screen name****" 
   ["x_auth_expires"]=> string(1) "0" 
}

在这里您看到最后一个元素是 ["x_auth_expires"],其值为 0.我认为这个元素没有出现在旧版本的库中.我想这就是导致问题的原因.

Here you see that the last element is ["x_auth_expires"] whose value is 0. I think this element did not appear in the older version of the library. And I suppose this is the thing which is causing the problem.

我尝试重新生成我的 Customer_KeyCustomer_Secret,但即使这样似乎也无济于事.

I tried re-generating my Customer_Key and Customer_Secret, but even that didn't seem to help.

任何形式的帮助将不胜感激.谢谢.

Any kind of help will be appreciated. Thank you.

推荐答案

终于找到了解决方案.

您需要做的就是,在获得回调后,使用新的访问令牌再次初始化该类.

All you need to do is, once you get the callback, initialize the class again with new access token.

$connection = new TwitterOAuth('MY_CONSUMER_KEY', 'MY_CONSUMER_SECRET', $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));

$connection = new TwitterOAuth('MY_CONSUMER_KEY', 'MY_CONSUMER_SECRET', $access_token['oauth_token'], $access_token['oauth_token_secret']);

我不知道为什么会这样,但它确实很有魅力.从此处找到此解决方案.

I don't know why that works, but it does work like a charm. Found this solution from here.

这篇关于Twitter OAuth:无效或过期的令牌[不重复]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆