/** * Gets the user data when a user is logged in * @throws BearerErrorResponseException When OAuth authentication failed * @return array|null The user data when a user is authenticated, or null when there is no user authenticated */ public function getUserData() { $accessToken = $this->getAccessToken(); if (!$accessToken) { return null; } $this->httpClient->addSubscriber(new BearerAuth($accessToken->getAccessToken())); try { $response = $this->httpClient->get($this->authserverUrl . '/api/user.json')->send()->json(); return $response; } catch (BearerErrorResponseException $ex) { $this->api->deleteAccessToken($this->context); $this->api->deleteRefreshToken($this->context); throw $ex; } }
$httpClient = new Client(); $api = new Api("php-voot-client", $clientConfig, $tokenStorage, $httpClient); $context = new Context("*****@*****.**", new Scope($config['scope'])); $accessToken = $api->getAccessToken($context); if (false === $accessToken) { /* no valid access token available, go to authorization server */ header("HTTP/1.1 302 Found"); header("Location: " . $api->getAuthorizeUri($context)); exit; } try { $client = new Client(); $bearerAuth = new BearerAuth($accessToken->getAccessToken()); $client->addSubscriber($bearerAuth); $response = $client->get($config['api_uri'])->send(); header("Content-Type: application/json"); echo $response->getBody(); } catch (BearerErrorResponseException $e) { if ("invalid_token" === $e->getBearerReason()) { // the token we used was invalid, possibly revoked, we throw it away $api->deleteAccessToken($context); $api->deleteRefreshToken($context); /* no valid access token available, go to authorization server */ header("HTTP/1.1 302 Found"); header("Location: " . $api->getAuthorizeUri($context)); exit; } throw $e; } catch (Exception $e) { die(sprintf('ERROR: %s', $e->getMessage())); }