您好我已将abraham / twitteroauth
用于推文API
给出错误
实际上,当我接受提供oauth_token和密钥时,twitter窗口会请求权限,但在我尝试发布时使用它会收到此错误。
Could not post Tweet. Error: 401 Reason: Could not authenticate you.
我的代码如下:
2个文件
<?php
session_start();
require_once 'classes/twitteroauth.php';
define("CONSUMER_KEY", "XXXXXXXXXXXXXXXX");
define("CONSUMER_SECRET", "XXXXXXXXXXXXXXXXXXXXXXX");
if(isset($_SESSION["oauth_token"])){
$ction = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,
$_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$tweetmsg = 'Hello World, I am tweeting from my own twitter app!';
$result = $ction->post('statuses/update', array('status' => $tweetmsg));
$httpCode = $ction->http_code;
if ($httpCode == 200) {
$resultmsg = 'Tweet Posted: '.$tweetmsg;
}
else {
$resultmsg = 'Could not post Tweet. Error: '.$httpCode.' Reason: '.$result->error;
}
} 其他{ 回声“有些该死的错误!”; } ?&GT;
第一页的HTML部分
<html>
<head>
<title>Twitter OAuth via popup</title>
</head>
<body>
<script src="jsjquery.min.js"></script>
<script src="js/jquery.oauthpopup.js"></script>
<script>
$(document).ready(function(){
$('#connect').click(function(){
$.oauthpopup({
path: 'twitter.php',
callback: function(){
alert("successfully Tweetted");
window.location.reload();
}
});
});
});
</script>
<div>
<?php
echo $resultmsg;
?>
</div>
<input type="button" value="Connect with Twitter" id="connect" /><br />
<a href="signout.php">Sign Out</a>
</body>
</html>
第二页
<?php
session_start();
require_once 'classes/twitteroauth.php';
if(!isset($_SESSION["oauth_token"])){
define("CONSUMER_KEY", "XXXXXXXXXXXXXXXX");
define("CONSUMER_SECRET", "XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->getRequestToken();
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
$url = $connection->getAuthorizeURL($request_token);
header('Location:'.$url);
die();
}
?>
<html>
<head>
<title>Share On Twitter</title>
</head>
<body>
<script type="text/javascript">
window.close();
</script>
</body>
</html>
答案 0 :(得分:3)
您错过了OAuth请求的第3部分,即将请求令牌与用户帐户的访问令牌交换。检查documentation of twitteroauth
中的#7<?php
session_start();
require_once 'classes/twitteroauth.php';
define("CONSUMER_KEY", "XXXXXXXXXXXXXXXX");
define("CONSUMER_SECRET", "XXXXXXXXXXXXXXXXXXXXXXX");
if(!$_SESSION['loggedin'] and $_SESSION['oauth_token']) {
# Exchange request token with access token
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$token_credentials = $connection->getAccessToken($_REQUEST['oauth_verifier']);
$_SESSION['oauth_token'] = $token_credentials['oauth_token'];
$_SESSION['oauth_token_secret'] = $token_credentials['oauth_token_secret'];
$_SESSION['loggedin'] = true;
}
if($_SESSION['loggedin']) {
$ction = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$tweetmsg = 'Hello World, I am tweeting from my own twitter app!';
$result = $ction->post('statuses/update', array('status' => $tweetmsg));
$httpCode = $ction->http_code;
if ($httpCode == 200) {
$resultmsg = 'Tweet Posted: '.$tweetmsg;
}
}
用上面的代码替换你的第一个文件。