/** * Pull in core sportily routes. */ public function boot() { # setup the api client library. Api::setBaseUrl(Config::get('sportily.base_url')); Api::setApiKeys(Config::get('sportily.client_id'), Config::get('sportily.client_secret')); Api::setRedirectUrl(Config::get('sportily.redirect_url')); # define routes for working with the oauth flow. include __DIR__ . '/Routing/routes.php'; # publish our config so that it can be imported into projects. $this->publishes([__DIR__ . '/Config/config.php' => config_path('sportily.php')]); }
/** * Exchange an auth code for an access token, then send the user back to * the home page, now logged in. * * @return mixed */ public function callback() { $auth_code = Input::get('code'); if ($auth_code) { $response = OAuth::token($auth_code); $access_token = $response['access_token']; $expiry_date = Carbon::now()->addSeconds($response['expires_in']); Session::set('access_token', $access_token); Session::set('access_token_expiry', $expiry_date); Api::setAccessToken($access_token); } return Redirect::to('/'); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $now = Carbon::now(); # look for a token in the session. list($token, $expiry) = $this->getSessionToken(); if ($token == null || $expiry->lte($now)) { # look for a token in the cache. list($token, $expiry) = $this->getCachedToken(); if ($token == null || $expiry->lte($now)) { # still nothing, then we'll have to get a new one. list($token, $expiry) = $this->getNewToken(); Cache::forever('access_token', $token); Cache::forever('access_token_expiry', $expiry); } } # store the access token in the session for later use. Session::set('access_token', $token); Session::set('access_token_expiry', $expiry); Api::setAccessToken($token); # permit the action. return $next($request); }
public function compose(View $view) { $view->with('access_token', Api::getAccessToken()); }
/** * User the guzzle client to perform a request to the API service. * * @param string $method one of 'get', 'post', 'put', or 'delete' * @param string $url the URL of the resource to request * @param array $payload the request body and/or query parameters * * @return array the raw JSON response */ public static function request($method, $url, $payload) { # ensure the access token is present in the headers. $token = Api::getAccessToken(); $payload['headers'] = ['Authorization' => 'Bearer ' . $token]; $payload['future'] = true; # create the request $request = self::client()->createRequest($method, $url, $payload); $response = self::client()->send($request); return new Response($response); }