当前位置: 首页 > 知识库问答 >
问题:

使用PHP的Twitter身份验证:#89无效或过期令牌

孔瑾瑜
2023-03-14
    null
require 'vendor/autoload.php';

use Codebird\Codebird;

class Twitter
{
    protected $consumer_key = 'xx';
    protected $consumer_secret = 'xx';
    protected $access_token = 'xx';
    protected $access_secret = 'xx';
    protected $twitter;

    public function __construct($key = null, $secret = null)
    {
        // Fetch new Twitter Instance
        Codebird::setConsumerKey($this->consumer_key, $this->consumer_secret);
        $this->twitter = Codebird::getInstance();

        // Set access token
        if(is_null($key)) :
            $this->setToken($this->access_token, $this->access_secret);
        else :
            $this->setToken($key, $secret);
        endif;
    }

    public function tweet( $message ) 
    {
        $params = array(
            'status' => $message
        );
        return $this->twitter->statuses_update($params);
    }

    public function tweetWithImage( $message, $image_url ) 
    {
        $params = array(
            'status' => $message,
            'media[]' => $image_url
        );
        return $this->twitter->statuses_updateWithMedia($params);
    }

    public function setToken( $key, $secret )
    {
        return $this->twitter->setToken($key, $secret);
    }

    public function getBearerToken( ) {
        return $this->twitter->oauth2_token();
    }

    public function getRequestToken($ident) {
        $reply = $this->twitter->oauth_requestToken(array(
            'oauth_callback' => 'http://api.exxica.com/publisher/twitter/authorize?ident='.$ident
        ));
        return $reply;
    }

    public function getUserData( $fields = false ) {
        return $this->twitter->account_verifyCredentials( array( 'include_entities' => $fields ) ); 
    }

    public function verifyToken( $oauth_verifier ) {
        $reply = $this->twitter->oauth_accessToken(array(
            'oauth_verifier' => $oauth_verifier
        ));
        return $reply;
    }

    public function generateTokens($ident) {
        // get the request token
        $reply = $this->getRequestToken($ident);

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

        // Stores the tokens
        $cr = new CDbCriteria();
        $cr->compare('user_id', $ident);
        $client = _Twitter::model()->find( $cr );
        if( is_null( $client ) ) $client = new _Twitter;
        $client->user_id = $ident;
        $client->access_token = $reply->oauth_token;
        $client->access_secret = $reply->oauth_token_secret;
        $client->save();
    }

    public function getAuthUrl() {
        return $this->twitter->oauth_authorize();
    }
}
            ...
                $twitter = new Twitter();
                $twitter->generateTokens($identity->id);
                $output = array( 'success' => true, 'loginUrl' => $twitter->getAuthUrl());
                $this->render('echo', array('response'=> $output ) );   
            ...
            ...
            // Receives redirect from Twitter
            if(!empty($_GET['oauth_verifier'])) {
                $oauth_verifier = $_GET['oauth_verifier'];
                $ident = $_GET['ident'];

                $cr = new CDbCriteria();
                $cr->compare( 'user_id', $ident );
                $client = _Twitter::model()->find( $cr );

                // Set the request token
                $twitter = new Twitter($client->access_token, $client->access_secret);

                $twitter_user = $twitter->getUserData(array('screen_name'));

                $client->user_id = $ident;
                $client->account_name = $twitter_user->screen_name;
                $client->lastused = date('Y-m-d H:i:s', time());
                $client->expires = date('Y-m-d H:i:s', strtotime('+2 months'));
                $client->save();
                $output = array( 
                    'success' => true, 
                    'twitter_user' => $twitter_user,
                    'client' => $client
                );

            } else {
                $output = array( 'success' => false );
            }

            $this->render('echo', array( 'response' => $output ) );
            ...
{
    "success": true,
    "twitter_user": {
        "errors": [
            {
                "message": "Invalid or expired token",
                "code": 89
            }
        ],
        "httpstatus": 401,
        "rate": null
    },
    "client": {
        "ID": "7",
        "user_id": "25",
        "account_name": null,
        "access_token": "xxxx",
        "access_secret": "xxxx",
        "created": "2014-10-20 10:59:06",
        "expires": "2014-12-29 09:06:22",
        "lastused": "2014-10-29 09:06:22"
    }
}

请记住,这段代码是经过一周调试的产物,因此可能缺少一些重要的部分。例如存储授权访问令牌。

共有1个答案

李昌勋
2023-03-14
...
                $twitter = new Twitter($client->access_token, $client->access_secret);

                $reply = $twitter->verifyToken( $oauth_verifier ); // Added this

                if($reply->httpstatus == 200) { // Added this IF
                    $client->user_id = $ident;
                    $client->account_name = $reply->screen_name;
                    $client->access_token = $reply->oauth_token;
                    $client->access_secret = $reply->oauth_token_secret;
                    $client->lastused = date('Y-m-d H:i:s', time());
                    $client->expires = date('Y-m-d H:i:s', strtotime('+2 months'));
                    $client->save();
                    $output = array( 
                        'success' => true, 
                        'client' => $client,
                        'reply' => $reply
                    );
                } else {
                    $output = array( 
                        'success' => false, 
                        'reply' => $reply
                    );
                }
...
{
    "success": true,
    "client": {
        "ID": "7",
        "user_id": "25",
        "account_name": USER_NAME,
        "access_token": USER_TOKEN,
        "access_secret": USER_SECRET,
        "created": "2014-10-20 10:59:06",
        "expires": "2014-12-29 10:26:46",
        "lastused": "2014-10-29 10:26:46"
    },
    "reply": {
        "oauth_token": USER_TOKEN,
        "oauth_token_secret": USER_SECRET,
        "user_id": USER_ID,
        "screen_name": USER_NAME,
        "httpstatus": 200,
        "rate": null
    }
}
 类似资料:
  • 这是代码,我正在重复错误401:身份验证错误

  • 在本章中,我们将解释如何使用Twitter身份验证。 第1步 - 创建Twitter应用程序 您可以在此link上创建Twitter应用程序。 创建应用程序后,单击“ Keys and Access Tokens ,您可以在其中找到API Key和API Secret 。 您将在第2步中使用此功能。 第2步 - 启用Twitter身份验证 在Firebase信息中心的侧边菜单中,您需要点击Auth

  • 我对社交网络分析和twitter api是新手。我想收集关于特定主题的tweets。所以我写了下面的代码 在我的程序中,我需要在哪里提供凭据 谢谢

  • 我已经创建了一个简单的spring boot应用程序,我已经使用KeyCloak对其进行了身份验证。当我调用服务(http://localhost:8080/table1data)时,它会将我重定向到keycloak登录页面,在该页面中插入我的领域用户名和密码,然后返回数据。现在到现在一切都很好。 我想要另一个场景。如果我的用户已经拥有相应领域的有效令牌,并且他希望使用正确的令牌访问相同的URL,

  • 所以我正忙着为twitter开发一个应用程序,我希望人们使用PIN进行身份验证。(https://dev.twitter.com/oauth/pin-based). 要向人们显示PIN码,我需要使用以下命令:https://dev.twitter.com/oauth/reference/get/oauth/authorize 那里的例子说:https://api.twitter.com/oauth

  • 我想使用 使用 来验证我的 REST 后端。您能否详细说明我如何将这种安全性集成到我的 Spring 应用程序中。 我希望使用与Spring Social Security相同的用户管理表和本地用户表。