有人可以帮我在twitter上发布状态吗?

时间:2011-03-24 20:27:54

标签: php oauth twitter twitter-oauth

我非常关注本教程 http://net.tutsplus.com/tutorials/php/how-to-authenticate-users-with-twitter-oauth/

并且我有一切正常工作我可以看到屏幕名称,当我调用它以及我的状态时间线但由于某种原因我无法发布状态。我在互联网上到处寻找,每个人都以同样的方式做到了,但我不能让我去工作。

这是我的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');
}
?>

和heres 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>

试图找出它是否已经更新但找不到任何东西,我猜它不是

1 个答案:

答案 0 :(得分:2)

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

在向用户发出API请求之前,

twitter_oauth.php还应该使用access_token构建一个新的TwitterOAuth对象。

如果获取访问令牌失败,您也没有错误处理。