我正在尝试从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);
}
}