dongrong9938 2014-01-09 20:00
浏览 65

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

I understand that this question is asked a lot of times, but yet, I still couldn't fix my issue with all the different solutions out there.

I trying to make use of this PHP class: https://github.com/abraham/twitteroauth

I've been manipulating with my code for quite a while and I got nothing, except these errors:

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] => )

This is very strange. I don't see anything that I could have done wrong. This is the code I've been writing:

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;
    }
}

So as you see, I'm attempting to keep all my code in one page. The error I've shown above comes from the this line: $token = $request_token['oauth_token'];. I've exited the script right before showing the whole error in order to print_r the array and get the real cause of this problem, which is

Failed to validate oauth signature and token

I'm stuck with this and I don't know how to continue. Any clues would be highly appreciated.

request_var() is equivalent to $_REQUEST
$user->get_data() gets data from the $_SESSION
$user->set_data() sets data to the $_SESSION
$user->quit(false) destroys the session

  • 写回答

1条回答 默认 最新

  • dp7311 2014-02-26 10:12
    关注

    If you are trying to authenticate other users, don't send the access_token and access_token_secret of your application when you ask for a request token.

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line