1

我已经创建了一个 Twitter 应用程序,可以在我的网站上使用 Twitter 登录,它工作得很好。如果我创建另一个具有相同设置的应用程序,我将无法登录。实际上,twitter 在提供的回调上重定向用户,我得到了一个访问令牌,但是当我调用“account/verify_credentials”时,我收到了这个错误消息:[message] => 无法验证你,[code] => 32。

    require_once 'twitter/twitteroauth.php';
    $twitter_app_data = UserModel::getLoginSourcesByCode('TWITTER');

    // Build TwitterOAuth object with client credentials.
    $connection = new TwitterOAuth($twitter_app_data['oauth_api'], $twitter_app_data['oauth_api_secret']);

    // Get temporary credentials.
    $request_token = $connection->getRequestToken($_REQUEST['callback']);

    // Save temporary credentials to session.
    $this->app->setEncryptedCookie('oauth_token', $request_token['oauth_token'], '15 minutes');
    $this->app->setEncryptedCookie('oauth_token_secret', $request_token['oauth_token_secret'], '15 minutes');

    // If last connection failed don't display authorization link.
    switch ($connection->http_code) {
      case 200:

        // Build authorize URL and redirect user to Twitter.
        $url = $connection->getAuthorizeURL($request_token['oauth_token'], '');
        $this->app->redirect($url, 303);
        break;
      default:
        //Unauthorized
    }

回调代码:

    require_once 'twitter/twitteroauth.php';
    // //get targetURL from cookie
    $error_message = '';

    // If the oauth_token is old redirect to the connect page.
    if (isset($_REQUEST['oauth_token']) && $this->app->getEncryptedCookie('oauth_token') !== $_REQUEST['oauth_token']) {
        $error_message = 'You are using an old access token. Please try again.';
    }else{
        $twitter_app_data = UserModel::getLoginSourcesByCode('TWITTER');

        // Create TwitteroAuth object with app key/secret and token key/secret from default phase
        $connection = new TwitterOAuth($twitter_app_data['oauth_api'], $twitter_app_data['oauth_api_secret'], $this->app->getEncryptedCookie('oauth_token'), $this->app->getEncryptedCookie('oauth_token_secret'));
        // Request access tokens from twitter
        $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);

        // If HTTP response is 200 continue otherwise send to connect page to retry
        if (200 == $connection->http_code) {
            $twitter_social_source = UserModel::getSocialSourcesByCode('TWITTER');

            // The user has been verified and the access tokens can be saved for future use
            $twitter_user = $connection->get('account/verify_credentials');
            //here is my problem !!!!!!!!!!!!!!
        } else {
            //redirect to targetURL (the original requested page)
            parent::logUserAction('406', 'login', 'Twitter login error. Http code: '.$connection->http_code.'. File '.__FILE__.', line '.__LINE__);
            $error_message = 'There was a glitch in the MATRIX. Please try again.';
        }
    }
4

0 回答 0