|
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\Amazon; |
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['amazon']['key'], |
27 | | - $servicesCredentials['amazon']['secret'], |
28 | | - $currentUri->getAbsoluteUri() |
29 | | -); |
30 | | - |
31 | | -// Instantiate the Amazon service using the credentials, http client, storage mechanism for the token and profile scope |
32 | | -/** @var Amazon $amazonService */ |
33 | | -$amazonService = $serviceFactory->createService('amazon', $credentials, $storage, ['profile']); |
34 | | - |
35 | | -if (!empty($_GET['code'])) { |
36 | | - // This was a callback request from Amazon, get the token |
37 | | - $token = $amazonService->requestAccessToken($_GET['code']); |
38 | | - |
39 | | - // Send a request with it |
40 | | - $result = json_decode($amazonService->request('/user/profile'), true); |
41 | | - |
42 | | - // Show some of the resultant data |
43 | | - echo 'Your unique Amazon user id is: ' . $result['user_id'] . ' and your name is ' . $result['name']; |
44 | | -} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') { |
45 | | - $url = $amazonService->getAuthorizationUri(); |
46 | | - header('Location: ' . $url); |
47 | | -} else { |
48 | | - $url = $currentUri->getRelativeUri() . '?go=go'; |
49 | | - echo "<a href='$url'>Login with Amazon!</a>"; |
| 23 | +$client = new CurlClient(); |
| 24 | + |
| 25 | +$helper->setTitle('Amazon'); |
| 26 | +if (empty($_GET)) { |
| 27 | + echo $helper->getContent(); |
| 28 | +} elseif (!empty($_GET['key']) && !empty($_GET['secret']) && $_GET['oauth'] !== 'redirect') { |
| 29 | + $credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl()); |
| 30 | + $vkService = new Amazon($credentials, $client, $storage); |
| 31 | + echo $helper->getHeader(); |
| 32 | + echo '<a href="'.$vkService->getAuthorizationUri().'">get access token</a>'; |
| 33 | + echo $helper->getFooter(); |
| 34 | +} elseif (!empty($_GET['code'])) { |
| 35 | + $credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl()); |
| 36 | + $vkService = new Amazon($credentials, $client, $storage); |
| 37 | + |
| 38 | + echo $helper->getHeader(); |
| 39 | + try { |
| 40 | + $token = $vkService->requestAccessToken($_GET['code']); |
| 41 | + echo 'access token: ' . $token->getAccessToken(); |
| 42 | + } catch (TokenResponseException $exception) { |
| 43 | + $helper->getErrorMessage($exception); |
| 44 | + } |
| 45 | + echo $helper->getFooter(); |
50 | 46 | } |
0 commit comments