2011-10-27 57 views
0

我目前正在網站上工作。將有幾種類型的授權,其中之一是「通過Twitter」。我正在使用亞伯拉罕威廉姆斯的TwitterOAuth圖書館。我會盡力解釋如何下面的腳本作品:Twitter OAuth 1.0。 Opera重定向失敗

  1. 當你「登陸」在的index.php彈出窗口(login.php中)按鈕打打開,並詢問批准的應用程序一起工作您的推特帳號
  2. 提交後,它將通過$ _GET參數中的「訪問令牌」重定向到api.twitter.com。
  3. 然後,twitter會授權該「標記」,並返回到在twitter應用程序的設置中定義的回調URL。

它的工作原理非常完美,除了在從api.twitter.com重定向到回調URL時,Opera有問題。似乎根本沒有執行重定向。

下面是來源:

的index.php

<?php 
session_start(); 
session_destroy(); 
?> 
<!DOCTYPE html> 
<html> 

    <head> 

     <meta charset="utf-8" /> 
     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> 
     <script type="text/javascript"> 
     function popup(url) 
     { 
      var addr = document.location.href.replace('index.php',''); 
      popUpObj = window.open(addr + url, 'Twitter', 'width=700,height=600,menubar=yes,status=yes'); 

     } 

     function transferdata(data) 
      { 
       if (data.screen_name) 
       { 
        (elem = document.getElementById('tw-login')).parentNode.removeChild(elem); 
        document.getElementById('menu').innerHTML = 'Hello, ' + data.screen_name; 
       } 
       else 
        document.getElementById('menu').innerHTML = 'Twitter didn\'t authorize you :('; 
      } 
     </script> 
     <style type="text/css"> 

     #tw-login 
     { 
      display: block; 
      background-color: #ccc; 
      text-align: center; 
      font-family: "Trebuchet MS", Vernanda, serif; 
      color: #fff; 
      margin: 5px; 
      padding: 4px 6px; 
      text-decoration: none; 
      width: 80px; 
     } 
     #tw-logout 
     { 
      display: block; 
      background-color: #ccc; 
      text-align: center; 
      font-family: "Trebuchet MS", Vernanda, serif; 
      color: #fff; 
      margin: 5px; 
      padding: 4px 6px; 
      text-decoration: none; 
      width: 80px; 
     } 

     </style> 

    </head> 

    <body> 
     <a id="tw-login" href="" onclick="popup('login.php');">Log in</a> 
     <a id="tw-logout" href="session_clear.php">Log out</a> 
     <div id="menu"> 

     </div>  
    </body> 
</html> 

的login.php

<?php 
session_start(); 
include 'twitteroauth/twitteroauth.php'; 
define('TWITTER_KEY', '*******'); 
define('TWITTER_KEY_SECRET', '*******'); 


$twitteroauth = new TwitterOAuth(TWITTER_KEY, TWITTER_KEY_SECRET); 
$requestToken = $twitteroauth->getRequestToken(); 
$_SESSION['oauth_token'] = $requestToken['oauth_token']; 
$_SESSION['oauth_token_secret'] = $requestToken['oauth_token_secret']; 

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

callback.php

<?php 
session_start(); 
include 'twitteroauth/twitteroauth.php'; 
define('TWITTER_KEY', '*******'); 
define('TWITTER_KEY_SECRET', '*******'); 

if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])) 
{ 
    $twitteroauth = new TwitterOAuth(TWITTER_KEY, TWITTER_KEY_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 
    $accessToken = $twitteroauth->getAccessToken($_GET['oauth_verifier']); 
    $_SESSION['access_token'] = $accessToken; 
    $userinfo = $twitteroauth->get('account/verify_credentials'); 
} 
else 
    header('Location: login.php'); 

?> 
<!DOCTYPE html> 
<html> 
    <head> 

     <meta charset="utf-8"> 
     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> 
     <script type="text/javascript"> 
     $(document).ready(SendToMain()); 

     function SendToMain() 
     { 
      if(window.opener != null || !window.opener.closed) 
      { 
       window.opener.transferdata(<?php echo json_encode($userinfo); ?>); 
       window.close(); 
      } 
     } 
     </script> 

    </head> 

    <body> 
    </body> 
</html> 

session_clear.php

<?php 
session_start(); 
session_destroy(); 
header('Location: index.php'); 
+0

如果這些API密鑰真的是祕密的,也許你應該從公共StackOverflow後刪除它們:-) – hallvors

回答

1

您的重定向URI不使用ssl。要解決此問題,請改用https。

0

你必須發送一些瀏覽器狀態碼301一樣或303,他們將重定向之前。嘗試這樣:

header("Status: 303"); 
header("Location: /home.php"); 
exit; 
+0

我的重定向工作正常。從api.twitter.com重定向到回調失敗。我不幸地無法訪問twitter.com資源:) – mintobit

+0

在這種情況下,您必須提供更多詳細信息 - 因爲不知道Twitter如何實際執行重定向,我們無法調試此:-(如果您不想給我們一個顯示問題的URL,或許您可以使用Fiddler HTTP調試器來探索Twitter發送到各種瀏覽器的內容?順便說一下,您正在Intranet或Internet上重定向到的站點? – hallvors