twitter oauth身份验证和发布推文不起作用

时间:2011-07-24 16:58:15

标签: php twitter oauth verification tweets

所以我试图通过我的应用程序发布用户的推文。每当我刚刚获得oauth_token和oauth_secret时,我都可以发布推文没有问题,但是,如果我尝试保存它们以便稍后发布推文,我会收到错误:

    object(stdClass)#5 (2) {
     ["error"]=>
      string(27) "Could not authenticate you."
       ["request"]=>
      string(23) "/1/statuses/update.json"
    }

这是我用来获取令牌的脚本:

<?php

require("config.php");
require("twitterOAuth.php");
session_start();

         if(!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) &&                                          !empty($_SESSION['oauth_token_secret'])){
    // We've got everything we need
} else {
    // Something's missing, go back to square 1
    //header('Location: new_index.php');
}

// TwitterOAuth instance, with two new parameters we got in twitter_login.php
$twitteroauth = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
// Let's request the access token

$oauth_token = $_SESSION['oauth_token'];
$oauth_secret = $_SESSION['oauth_token_secret'];

$access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);

//post tweet
$result = $twitteroauth->post('statuses/update', array('status' => 'asd '));


// Save it in a session var
$_SESSION['access_token'] = $access_token;
// Let's get the user's info
$user_info = $twitteroauth->get('account/verify_credentials');
?>

以下是我尝试使用令牌发送推文的脚本:

<?php
require("config.php");
require_once('twitterOAuth.php');
$oAuthToken     = $argv[1];
$oAuthSecret    = $argv[2];
$message = $argv[3];
$post_id = $argv[4];

// create a new instance
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, "$oAuthToken", "$oAuthSecret");

//send a tweet
$result = $tweet->post('statuses/update', $message);//array('status' => "$message"));
$tweet_id = $result['id_str'];

?>

有什么想法吗?我真的可以在这里使用一些帮助。它昨晚工作正常,现在突然间根本不起作用:/

令牌是否会在会话变量之后过期而无效?

2 个答案:

答案 0 :(得分:1)

/*Try this one it will work proper*/
session_start();
require("config.php");
require_once("twitterOAuth.php");

$access_token = $_SESSION['access_token'];//which you got from callback

/* Create a TwitterOauth object with consumer/user tokens. */
$tweet      = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

$tweet->post('direct_messages/new', array('text' => $messageBody, 'screen_name' => $screenName))

答案 1 :(得分:0)

尝试使用此代码(在代码的第二部分中):

<?php
session_start();
require("config.php");
require_once("twitterOAuth.php");

$oAuthToken = $_SESSION['oauth_token'];
$oAuthSecret = $_SESSION['oauth_token_secret'];

等等。这段代码对你有用吗?