-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added examples and reference to the blog post on how to write extractors
- Loading branch information
Luciano Mammino
committed
Feb 10, 2014
1 parent
eda4f3a
commit 003ec4d
Showing
3 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/** | ||
* Bootstrap the library | ||
*/ | ||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
/** | ||
* Setup error reporting | ||
*/ | ||
error_reporting(E_ALL); | ||
ini_set('display_errors', 1); | ||
|
||
/** | ||
* Setup the timezone | ||
*/ | ||
ini_set('date.timezone', 'Europe/Amsterdam'); | ||
|
||
/** | ||
* Create a new instance of the URI class with the current URI, stripping the query string | ||
*/ | ||
$uriFactory = new \OAuth\Common\Http\Uri\UriFactory(); | ||
$currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER); | ||
$currentUri->setQuery(''); | ||
|
||
/** | ||
* @var array A list of all the credentials to be used by the different services in the examples | ||
*/ | ||
$servicesCredentials = array( | ||
'facebook' => array( | ||
'key' => 'put_your_app_key_here', | ||
'secret' => 'put_your_app_secret_here', | ||
) | ||
); | ||
|
||
/** @var $serviceFactory \OAuth\ServiceFactory An OAuth service factory. */ | ||
$serviceFactory = new \OAuth\ServiceFactory(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/** | ||
* Example of retrieving an authentication token of the Facebook service | ||
* and extract user data. Based on the Lusitanian/PHPoAuthLib facebook example that can be | ||
* found here: https://github.com/Lusitanian/PHPoAuthLib/blob/master/examples/facebook.php | ||
*/ | ||
|
||
use OAuth\OAuth2\Service\Facebook; | ||
use OAuth\Common\Storage\Session; | ||
use OAuth\Common\Consumer\Credentials; | ||
use OAuth\UserData\ExtractorFactory; | ||
|
||
/** | ||
* Bootstrap the example | ||
*/ | ||
require_once __DIR__ . '/bootstrap.php'; | ||
|
||
// Session storage | ||
$storage = new Session(); | ||
|
||
// Setup the credentials for the requests | ||
$credentials = new Credentials( | ||
$servicesCredentials['facebook']['key'], | ||
$servicesCredentials['facebook']['secret'], | ||
$currentUri->getAbsoluteUri() | ||
); | ||
|
||
// Instantiate the Facebook service using the credentials, http client and storage mechanism for the token | ||
/** @var $facebookService Facebook */ | ||
$facebookService = $serviceFactory->createService('facebook', $credentials, $storage, array()); | ||
|
||
if (!empty($_GET['code'])) { | ||
// This was a callback request from facebook, get the token | ||
$token = $facebookService->requestAccessToken($_GET['code']); | ||
|
||
// Send a request with it | ||
$result = json_decode($facebookService->request('/me'), true); | ||
|
||
// Instantiate the facebook extractor | ||
$extractorFactory = new ExtractorFactory(); | ||
$facebookExtractor = $extractorFactory->get($facebookService); | ||
|
||
// Show some of the resultant data using the extractor | ||
echo 'Your unique facebook user id is: ' . $facebookExtractor->getUniqueId() . | ||
' and your name is ' . $facebookExtractor->getFullName(); | ||
|
||
} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') { | ||
$url = $facebookService->getAuthorizationUri(); | ||
header('Location: ' . $url); | ||
} else { | ||
$url = $currentUri->getRelativeUri() . '?go=go'; | ||
echo "<a href='$url'>Login with Facebook!</a>"; | ||
} |