为什么我的twitter oauth访问令牌无效/过期

时间:2013-01-24 13:25:10

标签: php twitter twitter-oauth

我正在使用Twitter将用户登录到一个网站,这似乎一直在努力,直到我尝试获取有效的访问令牌。

require("twitteroauth.php");
require 'twconfig.php';
session_start();

$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);
$request_token = $twitteroauth->getRequestToken('http://****/tw_response.php');

$oauth_token = $request_token['oauth_token'];
$_SESSION['oauth_token'] = $oauth_token;

$oauth_token_secret = $request_token['oauth_token_secret'];
$_SESSION['oauth_token_secret'] = $oauth_token_secret;

if ($twitteroauth->http_code == 200) {
    url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
    header('Location: '.$url);
} else {
    die('Something wrong happened.');
}

这似乎工作正常,将我重定向到Twitter登录并确认访问权限,之后它返回到tw_response.php(我的回调网址),网址中包含以下变量:

http://example.com/login.php?oauth_token=sO3X...yj0k&oauth_verifier=Ip6T...gALQ 

在tw_response.php中,我尝试获取访问令牌,但报告为无效。我尝试使用var_dump查看访问令牌的内容,如下所示:

require("twitteroauth.php");
require 'twconfig.php';
session_start();

$oauth_verifier = $_REQUEST['oauth_verifier'];
$oauth_token = $_SESSION['oauth_token'];
$oauth_token_secret = $_SESSION['oauth_token_secret'];

$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, $oauth_token, $oauth_token_secret);

$access_token = $twitteroauth->getAccessToken($data['oauth_verifier']);
var_dump($access_token);

var_dump的结果以“无效/过期令牌”结尾:

array(8) {
    ["oauth_url"] => string(104) ""1.0" encoding="UTF-8"?>/oauth/access_token?oauth_consumer_key=ceE...9Dg"
    ["oauth_nonce"]=> string(32) "c52...d07"
    ["oauth_signature"]=> string(28) "ry7...Fcc="
    ["oauth_signature_method"]=> string(9) "HMAC-SHA1"
    ["oauth_timestamp"]=> string(10) "1359031586"
    ["oauth_token"]=> string(40) "sO3...j0k"
    ["oauth_verifier"]=> string(43) "Ip6...ALQ"
    ["oauth_version"]=> string(63) "1.0 Invalid / expired Token "
}

4 个答案:

答案 0 :(得分:17)

$access_token = $twitteroauth->getAccessToken($data['oauth_verifier']);
var_dump($access_token);

$data神奇地来自哪里?您有变量$oauth_verifier,但请记住,如果这是您注册的回调网址,则不需要此项。

由于您在getAccessToken中使用了无效变量,因此会返回无效值。

使用TwitterOAuth的正确方法:

if (!isset($_GET["oauth_token"])) {
    // set these values in a config file somewhere.
    $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

    // append a ?. This is your callback URL if you specify something.
    $credentials = $twitter->getRequestToken("http://example.com/test.php?");

    // try and be a bit more elegant with the URL... This is a minimal example
    $url = $twitter->getAuthorizeUrl($credentials);
    echo $url;

    // these are temporary tokens that must be used to fetch the new,
    // permanent access tokens. store these in some way,
    // session is a decent choice.
    $_SESSION["token"] = $credentials["oauth_token"];
    $_SESSION["secret"] = $credentials["oauth_token_secret"];
} else {

    // use the user's previously stored temporary credentials here
    $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,
                    $_SESSION["token"], $_SESSION["secret"]);

    // uses the oauth_token (from the request) already.
    // you store these credentials in your database (see below).
    $credentials = $twitter->getAccessToken($_GET["oauth_verifier"]);

    // just a printout of credentials. store these, don't display them.
    echo "<pre>";
    var_dump($credentials);
    // valid credentials, provided you give the app access to them.
    echo "</pre>";
}

我只使用一个脚本进行回调以方便使用;如果您愿意,可以将相关部分拆分成多个脚本。(您可能应该这样做)。

对于您的数据库,凭据也包括Twitter用户的用户名。
修改Twitter is now allocating 64bit integers for user IDs。您应该将其存储为字符串,以确保在应用程序的每个部分都无法处理64位整数时,不会导致错误的用户ID和冲突。

array(4) {
  ["oauth_token"]=>
  string(50) "7041...wYupkS"
  ["oauth_token_secret"]=>
  string(42) "O9ENq...21B2fk"
  ["user_id"]=> // user ID. always the same, never changes (store this as ID)
  string(9) "..."
  ["screen_name"]=> // username. can change.
  string(11) "..."
}

因此,如果您想通过Twitter登录用户,而不明确地让他们登录您的网站,您可以使用$_SESSION(我使用数据库进行登录,如果您想要保存,我们建议使用这些数据库州) 在上面的脚本中,您可以将其添加到else块的末尾:

$_SESSION["token"] = $credentials["oauth_token"];
$_SESSION["secret"] = $credentials["oauth_secret"];
$_SESSION["username"] = $credentials["screen_name"];

如果你想给他们一个用户页面(如果你使用javascript,通过id_str抓住他们的用户ID),你也可以从GET account/verify_credentials获取用户的屏幕名称等等:

$user_array = $twitter->get("account/verify_credentials");

答案 1 :(得分:4)

我认为此链接可以帮助您

http://www.phpgang.com/twitter-oauth-in-php_175.html

答案 2 :(得分:1)

如果不付款,则只能创建开发环境(沙盒)。

我已经回答了here

计数仅适用于付费高级帐户,并且需要为高级访问付费。

Use this link to Apply for access

答案 3 :(得分:0)

如果您的OAuth流程在某一天工作但下一次失败,请检查计算机的时钟。我正在运行一个Vagrant盒子,它以某种方式将其时间设置为前一天,这导致Twitter API返回{&#34;代码&#34;:89,&#34;消息&#34;:&#34;无效或过期的令牌。&#34;}。这也可能显示为401时间戳超出范围。您可以使用此命令在Ubuntu中更新您的时钟:

sudo ntpdate time.nist.gov
如果您的系统无ntpdate,则

Alternative method

sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"