问题描述
我非常关注本教程http://net.tutsplus.com/教程/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.
这是我的 twitter_oauth.php
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_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
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
推荐答案
您正在将 access_token 保存到 $_SESSION['access_token']
在 twitter_oauth.php
中,然后在 twitter_update.php
中,您正在使用 $_SESSION['oauth_token']
.
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
还应使用 access_token 构建一个新的 TwitterOAuth
对象.
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.
这篇关于有人可以帮我在 Twitter 上发布状态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!