2014-01-09 89 views
0

據我所知,這個問題被問了很多次,但還沒有,我仍然無法與所有不同的解決方案解決了我的問題在那裏。的Twitter的OAuth:「無法驗證的OAuth簽名和令牌」使用PHP

我想利用這個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

如果你想驗證其他用戶,不送的access_token和請求令牌時,您的應用程序的access_token_secret。

相關問題