Skip to content

Commit 00b40e3

Browse files
author
Георгий Южаков
committed
update amazon example
1 parent f992e39 commit 00b40e3

4 files changed

Lines changed: 40 additions & 36 deletions

File tree

examples/provider/amazon.php

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,37 @@
1010
*/
1111

1212
use OAuth\Common\Consumer\Credentials;
13+
use OAuth\Common\Http\Client\CurlClient;
14+
use OAuth\Common\Http\Exception\TokenResponseException;
1315
use OAuth\Common\Storage\Session;
16+
use OAuth\Helper\Example;
1417
use OAuth\OAuth2\Service\Amazon;
1518

16-
/**
17-
* Bootstrap the example.
18-
*/
19-
require_once __DIR__ . '/bootstrap.php';
19+
require_once __DIR__.'/../bootstrap.php';
2020

21-
// Session storage
21+
$helper = new Example();
2222
$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();
5046
}

examples/provider/google.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
$helper = new Example();
1313
$storage = new Session();
1414
$client = new CurlClient();
15+
$helper->setTitle('Google');
1516

1617
if (empty($_GET)) {
1718
echo $helper->getContent();

examples/provider/vkontakte.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
use OAuth\Common\Storage\Session;
77
use OAuth\Helper\Example;
88
use OAuth\OAuth2\Service\Vkontakte;
9-
use OAuth\OAuth2\Token\StdOAuth2Token;
109

1110
require_once __DIR__.'/../bootstrap.php';
1211

1312
$helper = new Example();
1413
$storage = new Session();
1514
$client = new CurlClient();
16-
15+
$helper->setTitle('Vkontakte');
1716
if (empty($_GET)) {
1817
echo $helper->getContent();
1918
} elseif (!empty($_GET['key']) && !empty($_GET['secret']) && $_GET['oauth'] !== 'redirect') {

src/OAuth/Helper/Example.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Example
1313
* @var Finder
1414
*/
1515
private $finder;
16+
private $title;
1617

1718
public function __construct()
1819
{
@@ -33,10 +34,11 @@ public function getFinder(): Finder
3334

3435
public function getHeader(): string
3536
{
37+
$title = $this->title;
3638
return <<<HTML
3739
<html>
3840
<head>
39-
<title></title>
41+
<title>$title</title>
4042
<meta charset="utf-8">
4143
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
4244
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
@@ -67,6 +69,7 @@ public function getHeader(): string
6769
</div>
6870
</div>
6971
</div>
72+
<h1>$title</h1>
7073
HTML;
7174
}
7275

@@ -115,11 +118,16 @@ public function getCurrentUrl(): string
115118
return 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?oauth=redirect&key='. urldecode($_GET['key']) . '&secret=' . urldecode($_GET['secret']);
116119
}
117120

118-
public function getErrorMessage($exception)
121+
public function getErrorMessage($exception): void
119122
{
120123
echo '<div class="alert alert-danger">' . $exception->getMessage() . '</div>';
121124
echo '<pre>';
122125
print_r($exception) ;
123126
echo '</pre>';
124127
}
128+
129+
public function setTitle(string $title): void
130+
{
131+
$this->title = 'PHPoAuthLib - ' . $title;
132+
}
125133
}

0 commit comments

Comments
 (0)