2011-03-24 42 views
1

我幾乎下面這個教程 http://net.tutsplus.com/tutorials/php/how-to-authenticate-users-with-twitter-oauth/有人可以幫我在twitter上發佈狀態嗎?

和我有一切工作,我可以看到屏幕上的名字時,我把它和我的狀態的時間線,但由於某種原因,我不能發佈一個狀態。我在互聯網上無處不在,每個人都以同樣的方式做,但我不能讓我的工作。

這裏是我的twitter_oauth.php

<?php 
require("twitteroauth.php"); 
session_start(); 

if(!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])){ 
    $twitteroauth = new TwitterOAuth('consumerkey', 'otherkey', $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 
    //request the access token 
    $access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']); 
    $_SESSION['access_token'] = $access_token; 
    $user_info = $twitteroauth->get('account/verify_credentials'); 
    //useful for seeing array key names 
    //print_r($user_info); 

    mysql_connect('localhost', 'usernamr', 'password'); 
    mysql_select_db(' database_twitter'); 

    if(isset($user_info->error)){ 
     header('Location: twitter_login.php'); 
    }else{ 
     //find the user by its ID 
     $query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'twitter' AND oauth_uid = ". $user_info->id); 
     $result = mysql_fetch_array($query); 

     //if it doesnt exist add 
     if(empty($result)){ 
      $query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username, oauth_token, oauth_secret) VALUES ('twitter', {$user_info->id}, '{$user_info->screen_name}', '{$access_token['oauth_token']}', '{$access_token['oauth_token_secret']}')"); 
      $query = mysql_query("SELECT * FROM users WHERE id = " .mysql_insert_id()); 
      $result = mysql_fetch_array($query); 
     }else{ 
      // Update the tokens 
     $query = mysql_query("UPDATE users SET oauth_token = '{$access_token['oauth_token']}', oauth_secret = '{$access_token['oauth_token_secret']}' WHERE oauth_provider = 'twitter' AND oauth_uid = {$user_info->id}"); 
     } 

      $_SESSION['id'] = $result['id']; 
      $_SESSION['username'] = $result['username']; 
      $_SESSION['oauth_uid'] = $result['oauth_uid']; 
      $_SESSION['oauth_provider'] = $result['oauth_provider']; 
      $_SESSION['oauth_token'] = $result['oauth_token']; 
      $_SESSION['oauth_secret'] = $result['oauth_secret'];       



      header('Location: twitter_update.php'); 
    } 
}else{ 
    header('Location:twitter_login.php'); 
} 
?> 

和繼承人twitter_update.php

<?php 
require("twitteroauth.php"); 
session_start(); 
?> 
<h2>Hello <?php 
if(!empty($_SESSION['username'])){ 
    echo '@' .$_SESSION['username']; 
}else{ 
    echo 'Guest'; 
} 

if(!empty($_SESSION['username'])){ 
    $twitteroauth = new TwitterOAuth('key', 'key', $_SESSION['oauth_token'], $_SESSION['oauth_secret']); 
    $status_timeline = $twitteroauth->get('statuses/home_timeline', array('count' => 40)); 
    $content = $twitteroauth->get('account/verify_credentials'); 

    $twitteroauth->get('users/show', array('screen_name' => 'abraham')); 
    //$twitteroauth->post('statuses/update', array('status' => 'Testing out the twitter api with oauth')); 
    //print_r($status_timeline); 
    $twitteroauth->post('statuses/update', array('status' => "hello world")); 


} 
?> 
</h2> 

試圖找出它是否已被更新,但無法找到任何東西,我猜它不是

回答

2

您正在將access_token保存到$_SESSION['access_token'],twitter_oauth.php但是然後在twitter_update.php您正在使用$_SESSION['oauth_token']

twitter_oauth.php還應在以用戶身份向API發出請求之前,使用access_token構建新的TwitterOAuth對象。

在獲取訪問令牌失敗的情況下,您也沒有錯誤處理。

+0

非常感謝你檢查了一些我只是嘗試了很多不同的東西,我一定錯過了最明顯的。你是oauth的作者嗎? – 2011-03-25 01:52:58

+0

我是這個PHP庫的作者,但我不是OAuth規範的作者。 – abraham 2011-03-25 02:00:43

+0

我檢查了你說的話,並且我改變了會話名稱,因此它可以正常工作,因爲我可以抓取任何我無法發佈的內容,發佈什麼狀態?我甚至檢查了我的twitter帳戶連接,它顯示我的應用程序發送了我在發佈狀態中輸入的任何內容,但它不會發送它? – 2011-03-25 14:11:52

相關問題