OAuth::getAccessToken() 给我 401

发布于 2024-11-04 18:32:04 字数 1678 浏览 0 评论 0原文

我尝试从 Twitter 获取访问密钥,但在 getAccessToken() 处获取 401OAuth::getRequestToken() 工作正常。

知道为什么吗?

App::import('Core', 'CakeSession');

class TwitterSource extends DataSource {

    public $key;
    public $secret;
    private $requestTokenUrl = 'https://api.twitter.com/oauth/request_token';
    private $authorizeUrl = 'https://api.twitter.com/oauth/authorize';
    private $accessTokenUrl = 'https://api.twitter.com/oauth/access_token';

    public function __construct ($config) {
        $this->key = $config['key'];
        $this->secret = $config['secret'];
        $this->Session = new CakeSession();
        parent::__construct();
    }

    public function getAuthorizeUrl ($callback) {
        $o = new OAuth($this->key, $this->secret);
        $t = $o->getRequestToken($this->requestTokenUrl, $callback);
        $this->Session->write('twitter_request_token', $t);
        return $this->authorizeUrl.'?'.http_build_query(array(
            'oauth_token' => $t['oauth_token'],
            'oauth_callback' => $callback,
            'force_login' => false
        ));
    }

    public function getAccessToken () {
        $t = $this->Session->read('twitter_request_token');
        if ($t == null) {
            return false;
        }
        $oauth_verifier = isset($_GET['oauth_verifier']) ? $_GET['oauth_verifier'] : null;
        $o = new OAuth($this->key, $this->secret);
        $o->setToken($t['oauth_token'], $t['oauth_token_secret']);
        return $o->getAccessToken($this->accessTokenUrl, null, $oauth_verifier);
    }

}

I am trying to get an access key from Twitter, but get a 401 at getAccessToken().
OAuth::getRequestToken() works fine.

Any idea why?

App::import('Core', 'CakeSession');

class TwitterSource extends DataSource {

    public $key;
    public $secret;
    private $requestTokenUrl = 'https://api.twitter.com/oauth/request_token';
    private $authorizeUrl = 'https://api.twitter.com/oauth/authorize';
    private $accessTokenUrl = 'https://api.twitter.com/oauth/access_token';

    public function __construct ($config) {
        $this->key = $config['key'];
        $this->secret = $config['secret'];
        $this->Session = new CakeSession();
        parent::__construct();
    }

    public function getAuthorizeUrl ($callback) {
        $o = new OAuth($this->key, $this->secret);
        $t = $o->getRequestToken($this->requestTokenUrl, $callback);
        $this->Session->write('twitter_request_token', $t);
        return $this->authorizeUrl.'?'.http_build_query(array(
            'oauth_token' => $t['oauth_token'],
            'oauth_callback' => $callback,
            'force_login' => false
        ));
    }

    public function getAccessToken () {
        $t = $this->Session->read('twitter_request_token');
        if ($t == null) {
            return false;
        }
        $oauth_verifier = isset($_GET['oauth_verifier']) ? $_GET['oauth_verifier'] : null;
        $o = new OAuth($this->key, $this->secret);
        $o->setToken($t['oauth_token'], $t['oauth_token_secret']);
        return $o->getAccessToken($this->accessTokenUrl, null, $oauth_verifier);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文