开发者

Can somebody help me with posting a status on twitter?

I'm pretty much following this tutorial http://net.tutsplus.com/tutorials/php/how-to-authenticate-users-with-twitter-oauth/

and i have everything working i can see the screen name when i call it as well as my status time line but for some reason i can not post a status. i have looked everywhere on the internet and everybody does it the same way but i cant get mine to work.

here is my 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_se开发者_Go百科cret']}')");
            $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');
}
?>

and 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>

was trying to find out if it has been updated but couldn't find anything and i'm guessing it isn't


You are saving the access_token to $_SESSION['access_token'] in twitter_oauth.php but then in twitter_update.php you are using $_SESSION['oauth_token'].

twitter_oauth.php should also build a new TwitterOAuth object with the access_token before making requests to the API as the user.

You also have no error handling in the event that getting an access token fails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜