Twitter OAuth:“无法使用PHP验证oauth签名和令牌”

时间:2014-01-09 20:00:17

标签: php twitter oauth

我知道这个问题已被问过很多次了,但是,我仍然无法解决所有不同解决方案的问题。

我尝试使用这个PHP类:https://github.com/abraham/twitteroauth

我已经用我的代码操作了很长一段时间,除了这些错误之外我什么也没得到:

Notice: Undefined index: oauth_token in C:\xampp\htdocs\mywebsite\components\SocialConnect\twitter\twitteroauth.php on line 80

Notice: Undefined index: oauth_token_secret in C:\xampp\htdocs\mywebsite\components\SocialConnect\twitter\twitteroauth.php on line 80
Array ( [Failed to validate oauth signature and token] => )

这很奇怪。我没有看到任何我可能做错的事。这是我一直在写的代码:

include($mywebsite_root_path . 'components/SocialConnect/twitter/twitteroauth.php');

define('TWITTER_CONSUMER_KEY', '123');
define('TWITTER_CONSUMER_SECRET', 'abc');
define('TWITTER_OAUTH_CALLBACK', 'http://localhost/mywebsite/user/connect/twitter');

$oauth_token    = request_var('oauth_token', '');
$stored_token   = $user->get_data('oauth_token');

if (!empty($oauth_token))
{
    if ($oauth_token != $stored_token)
    {
        $user->set_data('oauth_status', 'oldtoken');
        $user->quit(false);
        redirect('user/connect/' . $service);
    }

    unset ($twitter);

    $secret_token = $user->get_data('oauth_token_secret');

    $twitter = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, $stored_token, $secret_token);

    /* Request access tokens from twitter */
    $oauth_verifier = request_var('oauth_verifier', ''); 
    $access_token   = $twitter->getAccessToken($oauth_verifier);

    /* Save the access tokens. Normally these would be saved in a database for future use. */
    $user->set_data('access_token', $access_token);

    /* Remove no longer needed request tokens */
    unset($oauth_token, $secret_token);
    $user->set_data('oauth_token', '');
    $user->set_data('oauth_token_secret', '');

    if (200 == $twitter->http_code) 
    {
        $user->set_data('status', 'verified');

        // Register here
        throw_msg("Welcome. Your account has been successfully connected with Twitter", true, null, 3, 'success');
    } 
    else 
    {
        $user->quit(false);
        throw_msg('Could not log into your Twitter account. Please try again.', true);
    }
}
else
{
    $twitter = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);

    /* Get temporary credentials. */
    $request_token = $twitter->getRequestToken(TWITTER_OAUTH_CALLBACK);

    print_r($request_token); exit;

    /* Save temporary credentials to session. */
    $token = $request_token['oauth_token'];
    $user->set_data('oauth_token', $token);
    $user->set_data('oauth_token_secret', $request_token['oauth_token_secret']);

    switch ($twitter->http_code) 
    {
        case 200:

            $url = $twitter->getAuthorizeURL($token);
            redirect($url, 'instant', null, true);

        break;

        default:
            throw_msg('Could not log into your Twitter account. Please try again.', true);
        break;
    }
}

如您所见,我正在尝试将所有代码保存在一个页面中。我上面显示的错误来自这一行:$token = $request_token['oauth_token'];。我在显示整个错误之前就退出了脚本,以便print_r数组并找到这个问题的真正原因,这是

  

无法验证oauth签名和令牌

我坚持这个,我不知道如何继续。任何线索都将受到高度赞赏。

request_var()相当于$_REQUEST
$user->get_data()$_SESSION获取数据 $user->set_data()将数据设置为$_SESSION $user->quit(false)会破坏会话

1 个答案:

答案 0 :(得分:1)

如果您尝试对其他用户进行身份验证,请不要在请求令牌时发送应用程序的access_token和access_token_secret。