2010-12-23 106 views
1

我已經創建了一個Twitter應用程序,我用它來發布推文。我無法解決的問題是每次必須允許訪問我的應用程序。PHP OAuth Twitter

因此可以說我需要推送三條消息,所以所有三次我必須允許訪問我的應用程序。

我只是需要一旦用戶允許訪問我的應用程序,下次他應該只被要求允許訪問是當他/她重新登錄時。

這裏是我的代碼,我使用

 
    Share content on twitter"; 

include 'lib/EpiCurl.php'; 
include 'lib/EpiOAuth.php'; 
include 'lib/EpiTwitter.php'; 
include 'lib/secret.php'; 

     $twitterObj = new EpiTwitter($consumer_key, $consumer_secret); 

     $oauth_token = $_GET['oauth_token']; 


if($oauth_token == '') 
{ 
    $url = $twitterObj->getAuthorizationUrl(); 
    echo ""; 
    echo "Sign In with Twitter"; 
    echo ""; 
} 
else 
{ 

     $twitterObj->setToken($_GET['oauth_token']); 
     $token = $twitterObj->getAccessToken(); 
     $twitterObj->setToken($token->oauth_token, $token->oauth_token_secret); 

     $_SESSION['ot'] = $token->oauth_token; 
     $_SESSION['ots'] = $token->oauth_token_secret; 
     $twitterInfo= $twitterObj->get_accountVerify_credentials(); 
     $twitterInfo->response; 

    $username = $twitterInfo->screen_name; 
    $profilepic = $twitterInfo->profile_image_url; 

    include 'update.php'; 
} 

if(isset($_POST['submit'])) 
{ 
    $msg = $_REQUEST['tweet']; 

    $twitterObj->setToken($_SESSION['ot'], $_SESSION['ots']); 
    $update_status = $twitterObj->post_statusesUpdate(array('status' => $msg)); 
    $temp = $update_status->response; 


    header("Location: MessageStatus.html"); 
    exit(); 
} 

?> 

回答

0
 
Share content on twitter"; 

include 'lib/EpiCurl.php'; 
include 'lib/EpiOAuth.php'; 
include 'lib/EpiTwitter.php'; 
include 'lib/secret.php'; 

     $twitterObj = new EpiTwitter($consumer_key, $consumer_secret); 

     $oauth_token = empty($_SESSION['ot']) ? $_GET['oauth_token']:$_SESSION['ot']; 


if($oauth_token == '') 
{ 
    $url = $twitterObj->getAuthorizationUrl(); 

     echo ""; 
     echo "Sign In with Twitter"; 
     echo ""; 


} 
else 
{ 
     if(empty($_SESSION['ot'])) 
     { 
      $twitterObj->setToken($_GET['oauth_token']); 
      $token = $twitterObj->getAccessToken(); 
      $twitterObj->setToken($token->oauth_token, $token->oauth_token_secret); 

      $_SESSION['ot'] = $token->oauth_token; 
      $_SESSION['ots'] = $token->oauth_token_secret; 
     } 
     else 
     { 
      $twitterObj->setToken($_SESSION['ot'], $_SESSION['ots']); 
     } 
     $twitterInfo= $twitterObj->get_accountVerify_credentials(); 
     $twitterInfo->response; 

    $username = $twitterInfo->screen_name; 
    $profilepic = $twitterInfo->profile_image_url; 

    include 'update.php'; 
} 

if(isset($_POST['submit'])) 
{ 
    $msg = $_REQUEST['tweet']; 
    try { 
    $twitterObj->setToken($_SESSION['ot'], $_SESSION['ots']); 
    $update_status = $twitterObj->post_statusesUpdate(array('status' => $msg)); 
    $temp = $update_status->response; 




    } 
    catch (Exception $e) 
    { 

    } 
} 

?>