php使用twitter登录

时间:2013-04-01 16:19:13

标签: php twitter oauth

我尝试使用epi twitter登录,这里是代码

    $consumerKey = $this->consumerKey ;
    $consumerSecret = $this->consumerSecret;        
    include 'application/libraries/twitter-oauth/EpiCurl.php';
    include 'application/libraries/twitter-oauth/EpiOAuth.php';
    include 'application/libraries/twitter-oauth/EpiTwitter.php';
    error_reporting(E_ALL);

    $Twitter = new EpiTwitter($consumerKey, $consumerSecret);

    if(isset($_GET['oauth_token']) || (isset($_COOKIE['oauth_token']) && isset($_COOKIE['oauth_token_secret'])))
    {
              // process
    }
    elseif(isset($_GET['denied']))
    {
        // user denied access
        echo 'You must sign in through twitter first';
        exit;
    }
    else
    {
        // user not logged in
        echo 'You are not logged in';
        exit;
    }   

但总是回复是“你没有登录”,尽管我已经在其他页面登录了Twitter。 我的代码有问题吗?

感谢任何帮助。

问候

1 个答案:

答案 0 :(得分:0)

验证用户身份时,请保存其authToken和authTokenSecret。 无论是在会话/数据库/ cookie中,都取决于您。

然后,当您创建EpiTwitter类的新实例时,将这两个新参数传递给swell。

$Twitter = new EpiTwitter($consumerKey, $consumerSecret, $userAuthToken, $userTokenSecret);

请参阅您正在调用的__construct函数的参数,

  public function __construct($consumerKey = null, $consumerSecret = null, $oauthToken = null, $oauthTokenSecret = null)
  {
    parent::__construct($consumerKey, $consumerSecret, self::EPITWITTER_SIGNATURE_METHOD);
    $this->setToken($oauthToken, $oauthTokenSecret);
  }