function actionReturn($code) { $url = "https://github.com/login/oauth/access_token?client_id=" . $this->app_key . "&redirect_uri=" . urlencode($this->url) . "&client_secret=" . $this->app_secret . "&code=" . $_GET['code'] . ""; $content = file_get_contents($url); $s = $content; $s = explode('&', $s); $d = explode('=', $s[0]); $access_token = $d[1]; if ($access_token) { try { $this->auth = OAuth2::provider($this->type, array('id' => $this->app_key, 'secret' => $this->app_secret)); $token = Token::factory('access', array('access_token' => $access_token)); $info = $this->auth->get_user_info($token); $uid = $info['uid']; $me['id'] = $uid; $me['name'] = $info['name']; $me['email'] = $info['emial']; $r = $this->member_get_third_set_user($me, $this->oauth_id, $access_token); flash('success', __('login success')); $this->redirect(return_url()); } catch (OAuthException $e) { flash('error', __('login error')); $this->redirect(return_url()); } } exit; }
function actionReturn() { $code = $_GET['code']; $postdata = http_build_query(array('grant_type' => 'authorization_code', 'client_id' => $this->app_key, 'client_secret' => $this->app_secret, 'redirect_uri' => urlencode($this->url), 'code' => $code)); $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata)); $context = stream_context_create($opts); $data = file_get_contents(Sohu::url_access_token(), false, $context); $data = json_decode($data); // $data = json_decode($data); // $access_token = $data->access_token; dump($data); exit; if ($access_token) { try { $this->auth = OAuth2::provider($this->type, array('id' => $this->app_key, 'secret' => $this->app_secret)); $token = Token::factory('access', array('access_token' => $access_token)); $info = $this->auth->get_user_info($token); if (!$info) { flash('error', __('login error')); $this->redirect(return_url()); exit; } $r = $this->member_get_third_set_user($info, $this->oauth_id, $access_token); flash('success', __('login success')); $this->redirect(return_url()); } catch (OAuthException $e) { flash('error', __('login error')); $this->redirect(return_url()); } } exit; }
function actionNext() { if ($access_token = $_GET['access_token']) { try { $this->auth = OAuth2::provider($this->type, array('id' => $this->app_key, 'secret' => $this->app_secret)); $token = Token::factory('access', array('access_token' => $access_token)); $info = $this->auth->get_user_info($token); $uid = $info['uid']; $me['id'] = $uid; $me['name'] = $info['name']; $me['email'] = $info['email']; $me['nickname'] = $info['nickname']; $r = $this->member_get_third_set_user($me, $this->oauth_id, $access_token); flash('success', __('login success')); $this->redirect(return_url()); } catch (OAuthException $e) { flash('error', __('login error')); $this->redirect(return_url()); } } }
public function access($code, $options = array()) { $params = array('client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'grant_type' => isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code'); switch ($params['grant_type']) { case 'authorization_code': $params['code'] = $code; $params['redirect_uri'] = isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri; break; case 'refresh_token': $params['refresh_token'] = $code; break; } $response = null; $url = $this->url_access_token(); if (!extension_loaded('openssl')) { exit('openssl not enable'); } switch ($this->method) { case 'GET': // Need to switch to Request library, but need to test it on one that works $url .= '?' . http_build_query($params); $response = file_get_contents($url); parse_str($response, $return); break; case 'POST': /* $ci = get_instance(); $ci->load->spark('curl/1.2.1'); $ci->curl ->create($url) ->post($params, array('failonerror' => false)); $response = $ci->curl->execute(); */ $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($params))); $_default_opts = stream_context_get_params(stream_context_get_default()); $context = stream_context_create(array_merge_recursive($_default_opts['options'], $opts)); $response = @file_get_contents($url, false, $context); $return = json_decode($response, true); break; default: throw new OutOfBoundsException("Method '{$this->method}' must be either GET or POST"); } if (!empty($return['error'])) { throw new OAuth2_Exception($return); } switch ($params['grant_type']) { case 'authorization_code': return Token::factory('access', $return); break; case 'refresh_token': return Token::factory('refresh', $return); break; } }