|
1 | 1 | <?php |
2 | 2 |
|
3 | | -/** |
4 | | - * Example of retrieving an authentication token of the Instagram service. |
5 | | - * |
6 | | - * PHP version 5.4 |
7 | | - * |
8 | | - * @author David Desberg <david@daviddesberg.com> |
9 | | - * @author Pieter Hordijk <info@pieterhordijk.com> |
10 | | - * @author Hannes Van De Vreken <vandevreken.hannes@gmail.com> |
11 | | - * @license http://www.opensource.org/licenses/mit-license.html MIT License |
12 | | - */ |
13 | | - |
14 | 3 | use OAuth\Common\Consumer\Credentials; |
| 4 | +use OAuth\Common\Http\Client\CurlClient; |
| 5 | +use OAuth\Common\Http\Exception\TokenResponseException; |
15 | 6 | use OAuth\Common\Storage\Session; |
| 7 | +use OAuth\Helper\Example; |
16 | 8 | use OAuth\OAuth2\Service\Instagram; |
17 | 9 |
|
18 | | -/** |
19 | | - * Bootstrap the example. |
20 | | - */ |
21 | | -require_once __DIR__ . '/bootstrap.php'; |
22 | 10 |
|
23 | | -// Session storage |
24 | | -$storage = new Session(); |
| 11 | +require_once __DIR__.'/../bootstrap.php'; |
25 | 12 |
|
26 | | -// Setup the credentials for the requests |
27 | | -$credentials = new Credentials( |
28 | | - $servicesCredentials['instagram']['key'], |
29 | | - $servicesCredentials['instagram']['secret'], |
30 | | - $currentUri->getAbsoluteUri() |
31 | | -); |
32 | | - |
33 | | -$scopes = ['basic', 'comments', 'relationships', 'likes']; |
34 | | - |
35 | | -// Instantiate the Instagram service using the credentials, http client and storage mechanism for the token |
36 | | -/** @var Instagram $instagramService */ |
37 | | -$instagramService = $serviceFactory->createService('instagram', $credentials, $storage, $scopes); |
38 | | - |
39 | | -if (!empty($_GET['code'])) { |
40 | | - // retrieve the CSRF state parameter |
41 | | - $state = $_GET['state'] ?? null; |
42 | | - |
43 | | - // This was a callback request from Instagram, get the token |
44 | | - $instagramService->requestAccessToken($_GET['code'], $state); |
45 | | - |
46 | | - // Send a request with it |
47 | | - $result = json_decode($instagramService->request('users/self'), true); |
48 | | - |
49 | | - // Show some of the resultant data |
50 | | - echo 'Your unique instagram user id is: ' . $result['data']['id'] . ' and your name is ' . $result['data']['full_name']; |
51 | | -} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') { |
52 | | - $url = $instagramService->getAuthorizationUri(); |
53 | | - header('Location: ' . $url); |
54 | | -} else { |
55 | | - $url = $currentUri->getRelativeUri() . '?go=go'; |
56 | | - echo "<a href='$url'>Login with Instagram!</a>"; |
| 13 | +$helper = new Example(); |
| 14 | +$storage = new Session(); |
| 15 | +$client = new CurlClient(); |
| 16 | + |
| 17 | +$helper->setTitle('Instagram'); |
| 18 | + |
| 19 | + |
| 20 | +if (empty($_GET)) { |
| 21 | + echo $helper->getContent(); |
| 22 | +} elseif (!empty($_GET['key']) && !empty($_GET['secret']) && $_GET['oauth'] !== 'redirect') { |
| 23 | + $credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl()); |
| 24 | + $service = new Instagram($credentials, $client, $storage); |
| 25 | + echo $helper->getHeader(); |
| 26 | + echo '<a href="'.$service->getAuthorizationUri().'">get access token</a>'; |
| 27 | + echo $helper->getFooter(); |
| 28 | +} elseif (!empty($_GET['code'])) { |
| 29 | + $credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl()); |
| 30 | + $service = new Instagram($credentials, $client, $storage); |
| 31 | + |
| 32 | + echo $helper->getHeader(); |
| 33 | + try { |
| 34 | + $token = $service->requestAccessToken($_GET['code']); |
| 35 | + echo 'access token: ' . $token->getAccessToken(); |
| 36 | + } catch (TokenResponseException $exception) { |
| 37 | + $helper->getErrorMessage($exception); |
| 38 | + } |
| 39 | + echo $helper->getFooter(); |
57 | 40 | } |
| 41 | + |
0 commit comments