Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Codebird PHP 220/403发布推文_Php_Twitter - Fatal编程技术网

使用Codebird PHP 220/403发布推文

使用Codebird PHP 220/403发布推文,php,twitter,Php,Twitter,当我试图从我的应用程序中推特一些东西时,我遇到了以下错误:object(stdClass)#6(2){[“errors”=>array(1){[0]=>object(stdClass){[“code”]=>int(220)[“message”]=>string(54)“您的凭据不允许访问此资源。”}[“httpstatus”]=>int(403)}” 我在youtube上学习了一个教程,在我尝试将它改为发布推文之前,它似乎工作得很好。有人能看看我的代码,看看我做错了什么吗?谢谢 class Tw

当我试图从我的应用程序中推特一些东西时,我遇到了以下错误:object(stdClass)#6(2){[“errors”=>array(1){[0]=>object(stdClass){[“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; ?>
require_once'app/init.php';
$auth=newtwitterauth($db$client);
?>
您已登录


任何想法都会有帮助

我看不到您在哪里设置使用者密钥/密钥对并获取Codebird实例:

Codebird::setConsumerKey($this->consumer_key, $this->consumer_secret);
$this->twitter = Codebird::getInstance();
可能是因为在这里发布而被删除了,但据我所知,你需要验证你的应用程序,否则你就没有发布的权限,不管你知道你的Twitter用户名和密码

此外,在Twitter开发站点上的应用程序配置中,您需要“读写”权限