0

我网站上的用户可以登录 Twitter 并立即在我的网站和 Twitter 上发布他们的状态。我正在使用https://github.com/abraham/twitteroauth连接到 Twitter。登录和发布是在网站的不同页面上进行的。

这是登录脚本:

public function loginTwitter() {
        $twitter = new TwitterOAuth(
                            $this->getContext()->params['social']['twitter']['consumerKey'],
                            $this->getContext()->params['social']['twitter']['consumerSecret']
            );
            $request_token = $twitter->getRequestToken($this->link('//User:connectFromTwitter'));

    // Saving to session (Nette Framework)
            $twitterSession = $this->getContext()->session->getSection('twSes');
            $twitterSession->oauth_request_token = $token = $request_token['oauth_token'];
            $twitterSession->oauth_request_token_secret = $request_token['oauth_token_secret'];

            if ($twitter->http_code == 200) {
                $requestLink = $twitter->getAuthorizeURL($token);
                $this->redirectUrl($requestLink);
            } else {
                echo 'Error';
            }
}

这是回调脚本(在用户登录后立即发布):

public function twitterOauth() {
    // $_GET parameter oauth_verifier
        $oauthVerifier = $this->getParam('oauth_verifier');

            // Session section
            $twitterSession = $this->getContext()->session->getSection('twSes');

            $twitter = new TwitterOAuth(
                            $this->getContext()->params['social']['twitter']['consumerKey'],
                            $this->getContext()->params['social']['twitter']['consumerSecret'],
                            $twitterSession->oauth_request_token,
                            $twitterSession->oauth_request_token_secret
            );

            $access_token = $twitter->getAccessToken($oauthVerifier);
            $twitterSession->access_token = $access_token;
            $user_info = $twitter->get('account/verify_credentials');

            // Saving to DB to be able to post without login
            $tm = new TwitterUserManager();
            if (!$tm->isInDatabase($this->getUser()->getId())) {
                $tu = new TwitterUser();
                $tu->setUser($this->loggedUser);
                $tu->setOauthProvider('twitter');
                $tu->setOauthUid("'".$user_info->id."'");
                $tu->setUsername("'".$user_info->screen_name."'");
                $tu->setOauthToken("'".$access_token['oauth_token']."'"); // Saving the access token for further posting
                $tu->setOauthSecret("'".$access_token['oauth_token_secret']."'");
                $tm->persist($tu);
            }

            $twitter->post('statuses/update', array('status' => 'Hello ' . date('d.m.Y H:i:s'))); // <== HERE IT WORKS

            $this->redirect('User:socialConnect'); // Redirect to another page
}

这是发布功能(来自任何页面的用户帖子):

    public function postToTwitter() {
        $twitterSession = $this->getContext()->session->getSection('twitter');

            $twitter = new TwitterOAuth(
                            $this->getContext()->params['social']['twitter']['consumerKey'],
                            $this->getContext()->params['social']['twitter']['consumerSecret'],
                            $twitterSession->access_token['oauth_token'],
                            $twitterSession->access_token['oauth_token_secret']
            );

            return $twitter->post('statuses/update', array('status' => 'Hello' . date('d.m.Y H:i:s')));
}

当我使用发布功能时,我收到此错误:

stdClass(2) {
       request => "/1/statuses/update.json" (23)
       error => "Could not authenticate you." (27)
    }

提前感谢您的帮助。

编辑:解决方案:

使用它连接到 Twitter(将所有信息保存到数据库中): http: //framework.zend.com/manual/1.12/en/zend.oauth.introduction.html

使用它从任何页面 发布:http: //framework.zend.com/manual/1.12/en/zend.service.twitter.html

很好的例子: http ://www.joeyrivera.com/2010/twitter-api-oauth-authentication-and-zend_oauth-tutorial/

4

1 回答 1

2

我一直使用 Zend-Framework-Component: http: //framework.zend.com/manual/1.12/en/zend.service.twitter.html

我认为这很简单,我可以确认它有效。您只需阅读本教程(请参阅上面的链接)。

于 2012-12-18T08:08:53.010 回答