开发者

Twitter API giving error

开发者 https://www.devze.com 2023-02-08 05:28 出处:网络
Hi i\'ve used abraham / twitteroauth for tweet API Gives the error Actually the twitter window comes asks for permission when i accepts gives a oauth_token and secret key but using that when i try

Hi i've used abraham / twitteroauth for tweet API

Gives the error

Actually the twitter window comes asks for permission when i accepts gives a oauth_token and secret key but using that when i try to post gets this error.

Could not post Tweet. Error: 401 Reason: Could not authenticate you.

My code is given below

2 files

<?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;
}

} else{ echo "Some damn error!"; } ?>

The HTML PART OF FIRST PAGE

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

SECOND PAGE

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


You missed the 3rd part of an OAuth request, which is to exchange the request token with the access token for the user account. Check #7 in the documentation of twitteroauth

<?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;
    }
}

Replace your first file with the code above.

0

精彩评论

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