|
10 | 10 | */ |
11 | 11 |
|
12 | 12 | use OAuth\Common\Consumer\Credentials; |
| 13 | +use OAuth\Common\Http\Client\CurlClient; |
| 14 | +use OAuth\Common\Http\Exception\TokenResponseException; |
13 | 15 | use OAuth\Common\Storage\Session; |
| 16 | +use OAuth\Helper\Example; |
14 | 17 | use OAuth\OAuth2\Service\Ustream; |
15 | 18 |
|
16 | | -/** |
17 | | - * Bootstrap the example. |
18 | | - */ |
19 | | -require_once __DIR__ . '/bootstrap.php'; |
| 19 | +require_once __DIR__ . '/../bootstrap.php'; |
20 | 20 |
|
21 | | -// Session storage |
| 21 | +$helper = new Example(); |
22 | 22 | $storage = new Session(); |
23 | | - |
24 | | -// Setup the credentials for the requests |
25 | | -$credentials = new Credentials( |
26 | | - $servicesCredentials['ustream']['key'], |
27 | | - $servicesCredentials['ustream']['secret'], |
28 | | - $currentUri->getAbsoluteUri() |
29 | | -); |
30 | | - |
31 | | -// Instantiate the Ustream service using the credentials, http client and storage mechanism for the token |
32 | | -/** @var Ustream $ustream */ |
33 | | -$ustream = $serviceFactory->createService('Ustream', $credentials, $storage, ['identity']); |
34 | | - |
35 | | -if (!empty($_GET['code'])) { |
36 | | - // retrieve the CSRF state parameter |
37 | | - $state = $_GET['state'] ?? null; |
38 | | - |
39 | | - // This was a callback request from Ustream, get the token |
40 | | - $ustream->requestAccessToken($_GET['code'], $state); |
41 | | - |
42 | | - $result = json_decode($ustream->request('users/self.json'), true); |
43 | | - |
44 | | - echo 'Your unique Ustream user id is: ' . $result['id'] . ' and your username is ' . $result['username']; |
45 | | -} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') { |
46 | | - $url = $ustream->getAuthorizationUri(); |
47 | | - header('Location: ' . $url); |
48 | | -} else { |
49 | | - $url = $currentUri->getRelativeUri() . '?go=go'; |
50 | | - echo "<a href='$url'>Login with Ustream!</a>"; |
| 23 | +$client = new CurlClient(); |
| 24 | + |
| 25 | +$helper->setTitle('Instagram'); |
| 26 | + |
| 27 | +if (empty($_GET)) { |
| 28 | + echo $helper->getContent(); |
| 29 | +} elseif (!empty($_GET['key']) && !empty($_GET['secret']) && $_GET['oauth'] !== 'redirect') { |
| 30 | + $credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl()); |
| 31 | + $service = new Ustream($credentials, $client, $storage); |
| 32 | + echo $helper->getHeader(); |
| 33 | + echo '<a href="' . $service->getAuthorizationUri() . '">get access token</a>'; |
| 34 | + echo $helper->getFooter(); |
| 35 | +} elseif (!empty($_GET['code'])) { |
| 36 | + $credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl()); |
| 37 | + $service = new Ustream($credentials, $client, $storage); |
| 38 | + |
| 39 | + echo $helper->getHeader(); |
| 40 | + |
| 41 | + try { |
| 42 | + $token = $service->requestAccessToken($_GET['code']); |
| 43 | + echo 'access token: ' . $token->getAccessToken(); |
| 44 | + } catch (TokenResponseException $exception) { |
| 45 | + $helper->getErrorMessage($exception); |
| 46 | + } |
| 47 | + echo $helper->getFooter(); |
51 | 48 | } |
0 commit comments