public function action_do()
{
$params = Oauth2::parse_query();
try {
if (empty($params['code']) or isset($params['error'])) {
throw new Oauth2_Exception($params['error']);
}
$token = Remote::get($this->_configs['token_uri'], array(CURLOPT_POST => TRUE, CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded;charset=utf-8'), CURLOPT_POSTFIELDS => Oauth2::build_query(array('grant_type' => $this->_configs['grant_type'], 'code' => $params['code'], 'client_id' => $this->_configs['client_id'], 'redirect_uri' => $this->_configs['redirect_uri'], 'client_secret' => $this->_configs['client_secret']))));
$token = json_decode($token);
if (isset($token->error)) {
throw new Oauth2_Exception($token->error);
}
// Resource in json format
$resource = Remote::get($this->_configs['access_uri'], array(CURLOPT_POST => TRUE, CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded;charset=utf-8'), CURLOPT_POSTFIELDS => Oauth2::build_query(array('oauth_token' => $token->access_token, 'timestamp' => $_SERVER['REQUEST_TIME'], 'refresh_token' => $token->refresh_token, 'expires_in' => $token->expires_in, 'client_id' => $this->_configs['client_id']))));
$this->request->response = $resource;
} catch (Exception $e) {
$error = $e->getMessage();
}
if (isset($error)) {
switch ($error) {
case 'access_denied':
$this->request->response = 'You have denied this request.';
break;
default:
$this->request->response = 'There must be some errors happen in this connection, please contact our web master.' . "[{$error}]";
break;
}
}
}