使用Codebird PHP 220/403发布推文

时间:2014-06-21 14:22:44

标签: php twitter

尝试从我的应用程序发送推文时出现以下错误:object(stdClass)#6(2){[“errors”] => array(1){[0] => object(stdClass)#7(2){[“code”] => int(220)[“message”] => string(54)“您的凭据不允许访问此资源。” } [“httpstatus”] => int(403)}

我在youtube上关注了一个教程,它似乎工作正常,直到我试图将其更改为发布推文。有人可以查看我的代码,看看我在这里做错了什么?感谢

class TwitterAuth
{

protected $db;

protected $client;

protected $clientCallback = 'http://www.twittest.intelwalk.com/callback.php';

public function __construct(DB $db, \Codebird\Codebird $client)
{
    $this->db=$db;
    $this->client=$client;
}

public function getAuthUrl()
{
    $this->requestTokens();
    $this->verifyTokens();

    return $this->client->oauth_authenticate();
}

public function signedIn()
{

    return isset($_SESSION['user_id']);
}

public function signIn()
{
    if($this->hasCallback())
    {
        $this->verifyTokens();

        $reply = $this->client->oauth_accessToken([
            'oauth_verifier' => $_GET['oauth_verifier']
            ]);

        if($reply->httpstatus === 200)
        {

            $this->storeTokens($reply->oauth_token, $reply->oauth_token_secret);

            $_SESSION['user_id'] = $reply->user_id;

            $this->storeUser($reply);

            return true;
        }
    }

    return false;
}

public function signOut()
{
    unset($_SESSION['user_id']);
}

protected function hasCallback()
{
    return isset($_GET['oauth_verifier']);
}

protected function requestTokens()
{
    $reply = $this->client->oauth_requestToken([

        'oauth_callback' => $this->clientCallback

        ]);
    $this->storeTokens($reply->oauth_token,$reply->oauth_token_secret);
}

protected function storeTokens($token,$tokenSecret)
{
    $_SESSION['oauth_token'] = $token;
    $_SESSION['oauth_token_secret'] = $tokenSecret;
}

protected function verifyTokens()
{
    $this->client->setToken($_SESSION['oauth_token'],$_SESSION['oauth_token_secret']);

}

protected function storeUser($payload)
{
    $this->db->query("

        INSERT INTO users (twitter_id, twitter_username)
        VALUES ({$payload->user_id},'{$payload->screen_name}')
        ON DUPLICATE KEY UPDATE id = id
        ");

}

public function tweet($message)
{
    $reply =  $this->client->statuses_update(['status' => $message]);

    var_dump($reply);

这是我的索引:

require_once 'app/init.php';

$auth = new TwitterAuth($db,$client);





?>

<?php if($auth->signedIn()): ?>
    <p>You are signed in. <a href="signout.php">Sign out</a></p>

<?php echo 'Test';
$auth->tweet('Hello World');
echo 'Test'; ?>
<?php else: ?>
    <p><a href="<?php echo $auth->getAuthUrl(); ?>">Sign in with Twitter</a></p>

<?php endif; ?>

任何想法都会有所帮助!

1 个答案:

答案 0 :(得分:0)

我不知道您在哪里设置消费者密钥/密钥对并获取Codebird实例:

Codebird::setConsumerKey($this->consumer_key, $this->consumer_secret);
$this->twitter = Codebird::getInstance();

也许已经删除了在此发布,但据我了解,您需要对您的应用进行身份验证,否则您无权发布,无论您是否知道自己的Twitter用户名和密码。

此外,在Twitter的开发网站上的应用配置中,您需要&#34;读写&#34;权限。