public function authorizeCallback() { // If there is an error parameter, show that error $error = $this->request->get('error'); if (!empty($error)) { HtmlPage::renderError5xx(500, "<pre>OAuth Error: " . $this->request->get('error') . "\n" . '<a href="/authorize_callback">Retry</a></pre>'); return; } // Get OAuth2 settings $authorizeUrl = Config::get('oauth.authorization_url'); $clientId = Config::get('oauth.client'); $clientSecret = Config::get('oauth.secret'); $redirectUrl = Config::get('oauth.redirect_uri'); $userAgent = Config::get('oauth.user_agent'); // Prepare OAuth2 client to request an authorization code $client = new Client($clientId, $clientSecret, Client::AUTH_TYPE_AUTHORIZATION_BASIC); $client->setCurlOption(CURLOPT_USERAGENT, $userAgent); // Request an authorization code if there isn't one in the GET // parameter code, if there is one, request an access token $code = $this->request->get('code'); if (empty($code)) { $this->session->delete('accessToken'); $authUrl = $client->getAuthenticationUrl($authorizeUrl, $redirectUrl, array('scope' => 'identity', 'state' => 'As64xA3ueT6sjxiazAA7278yhs6103jx', 'duration' => 'permanent')); header('Location: ' . $authUrl); } else { $this->session->requestOAuth2AccessToken($code); header('Location: /'); return; } }
/** * Here you can render the homepage of the app */ public function index() { // Get OAuth2 parameters from config and session $clientId = Config::get('oauth.client'); $clientSecret = Config::get('oauth.secret'); $userAgent = Config::get('oauth.user_agent'); $accessTokenResult = $this->session->read('accessToken'); // Setup OAuth2 client to request resources from Reddit $client = new Client($clientId, $clientSecret, Client::AUTH_TYPE_AUTHORIZATION_BASIC); $client->setCurlOption(CURLOPT_USERAGENT, $userAgent); $client->setAccessToken($accessTokenResult["access_token"]); $client->setAccessTokenType(Client::ACCESS_TOKEN_BEARER); // Request user response $response = $client->fetch("https://oauth.reddit.com/api/v1/me.json"); $this->view->render("Home", array('me' => $response['result'], 'pageTitle' => 'Reddit profile example')); }
/** * Requests an OAuth2 access token and saves it in the Session * as an array representing the response and with key "accessToken". * * @param $code * @throws \OAuth2\Exception */ public function requestOAuth2AccessToken($code) { // Get OAuth2 settings $accessTokenUrl = Config::get('oauth.access_token_url'); $clientId = Config::get('oauth.client'); $clientSecret = Config::get('oauth.secret'); $redirectUrl = Config::get('oauth.redirect_uri'); $userAgent = Config::get('oauth.user_agent'); // Prepare OAuth2 client $client = new Client($clientId, $clientSecret, Client::AUTH_TYPE_AUTHORIZATION_BASIC); $client->setCurlOption(CURLOPT_USERAGENT, $userAgent); // Get access token $accessTokenResult = $this->read('accessToken'); if (null == $accessTokenResult) { $params = array('code' => $code, "redirect_uri" => $redirectUrl); $response = $client->getAccessToken($accessTokenUrl, "authorization_code", $params); $accessTokenResult = $response["result"]; $this->store('accessToken', $accessTokenResult); } // How to request any resource from Reddit // $client->setAccessToken($accessTokenResult["access_token"]); // $client->setAccessTokenType(Client::ACCESS_TOKEN_BEARER); // $this->model->response = $client->fetch("https://oauth.reddit.com/api/v1/me.json"); }