OAuth::getAccessToken() 给我 401
我尝试从 Twitter 获取访问密钥,但在 getAccessToken()
处获取 401。 OAuth::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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论