function __construct($token = null) { if ($token != null) { $this->access_token = $token->access_token; $this->refresh_token = $token->refresh_token; $this->expired_in = $token->expired_in; if ($this->getAccessTokenStatus() != 1) { $keyValueStore = new KeyValueStore(new MemoryAdapter()); $keyValueStore->set('access_token', $this->access_token); $keyValueStore->set('refresh_token', $this->refresh_token); $keyValueStore->expire('access_token', 0); $keyValueStore->expire('refresh_token', $this->expired_in + (5184000 - 3600) - time()); # 60 days $oAuthClient = new OAuthClient($keyValueStore, $this->clientId, $this->clientSecret, $this->redirectUri); $oAuthClient->authorize(); $keyValueStore = $oAuthClient->getKvs(); Token::where('access_token', $this->access_token)->where('refresh_token', $this->refresh_token)->update(array('access_token' => $keyValueStore->get('access_token'), 'refresh_token' => $keyValueStore->get('refresh_token'), 'expired_in' => time() + $keyValueStore->getTtl('access_token'))); $this->access_token = $keyValueStore->get('access_token'); $this->refresh_token = $keyValueStore->get('refresh_token'); $this->expired_in = time() + $keyValueStore->getTtl('access_token'); } } else { $keyValueStore = new KeyValueStore(new MemoryAdapter()); $oAuthClient = new OAuthClient($keyValueStore, $this->clientId, $this->clientSecret, $this->redirectUri); try { $oAuthClient->authorize(); $keyValueStore = $oAuthClient->getKvs(); $this->access_token = $keyValueStore->get('access_token'); $this->refresh_token = $keyValueStore->get('refresh_token'); $this->expired_in = time() + $keyValueStore->getTtl('access_token'); } catch (ExitException $e) { # Location header has set (box's authorize page) # Instead of an exit call it throws an ExitException exit; } catch (OAuthException $e) { # e.g. Invalid user credentials # e.g. The user denied access to your application } catch (ClientException $e) { # e.g. if $_GET['code'] is older than 30 sec } } }
<?php require_once '/vendor/autoload.php'; use AdammBalogh\Box\Client\OAuthClient; use AdammBalogh\KeyValueStore\KeyValueStore; use AdammBalogh\KeyValueStore\Adapter\NullAdapter; use AdammBalogh\Box\Exception\ExitException; use AdammBalogh\Box\Exception\OAuthException; use GuzzleHttp\Exception\ClientException; $clientId = 'ovfbhzo5niff7zog2joq53pkocb544uc'; $clientSecret = '4Nw8sSNI2OQediWzn3VgyZeqYzqNKbur'; $redirectUri = 'http://localhost/gtc/authenBox.php'; $keyValueStore = new KeyValueStore(new NullAdapter()); $oAuthClient = new OAuthClient($keyValueStore, $clientId, $clientSecret, $redirectUri); try { $oAuthClient->authorize(); } catch (ExitException $e) { # Location header has set (box's authorize page) # Instead of an exit call it throws an ExitException exit; } catch (OAuthException $e) { # e.g. Invalid user credentials # e.g. The user denied access to your application } catch (ClientException $e) { # e.g. if $_GET['code'] is older than 30 sec } $accessToken = $oAuthClient->getAccessToken(); session_start(); $_SESSION['boxToken'] = $accessToken; header("Location: http://localhost/gtc/index.php"); die;