|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Example of retrieving an authentication token of the Facebook service |
| 5 | + * and extract user data. Based on the Lusitanian/PHPoAuthLib facebook example that can be |
| 6 | + * found here: https://github.com/Lusitanian/PHPoAuthLib/blob/master/examples/facebook.php |
| 7 | + */ |
| 8 | + |
| 9 | +use OAuth\OAuth2\Service\Facebook; |
| 10 | +use OAuth\Common\Storage\Session; |
| 11 | +use OAuth\Common\Consumer\Credentials; |
| 12 | +use OAuth\UserData\ExtractorFactory; |
| 13 | + |
| 14 | +/** |
| 15 | + * Bootstrap the example |
| 16 | + */ |
| 17 | +require_once __DIR__ . '/bootstrap.php'; |
| 18 | + |
| 19 | +// Session storage |
| 20 | +$storage = new Session(); |
| 21 | + |
| 22 | +// Setup the credentials for the requests |
| 23 | +$credentials = new Credentials( |
| 24 | + $servicesCredentials['facebook']['key'], |
| 25 | + $servicesCredentials['facebook']['secret'], |
| 26 | + $currentUri->getAbsoluteUri() |
| 27 | +); |
| 28 | + |
| 29 | +// Instantiate the Facebook service using the credentials, http client and storage mechanism for the token |
| 30 | +/** @var $facebookService Facebook */ |
| 31 | +$facebookService = $serviceFactory->createService('facebook', $credentials, $storage, array()); |
| 32 | + |
| 33 | +if (!empty($_GET['code'])) { |
| 34 | + // This was a callback request from facebook, get the token |
| 35 | + $token = $facebookService->requestAccessToken($_GET['code']); |
| 36 | + |
| 37 | + // Send a request with it |
| 38 | + $result = json_decode($facebookService->request('/me'), true); |
| 39 | + |
| 40 | + // Instantiate the facebook extractor |
| 41 | + $extractorFactory = new ExtractorFactory(); |
| 42 | + $facebookExtractor = $extractorFactory->get($facebookService); |
| 43 | + |
| 44 | + // Show some of the resultant data using the extractor |
| 45 | + echo 'Your unique facebook user id is: ' . $facebookExtractor->getUniqueId() . |
| 46 | + ' and your name is ' . $facebookExtractor->getFullName(); |
| 47 | + |
| 48 | +} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') { |
| 49 | + $url = $facebookService->getAuthorizationUri(); |
| 50 | + header('Location: ' . $url); |
| 51 | +} else { |
| 52 | + $url = $currentUri->getRelativeUri() . '?go=go'; |
| 53 | + echo "<a href='$url'>Login with Facebook!</a>"; |
| 54 | +} |
0 commit comments