Created
March 17, 2012 22:54
-
-
Save larryrubin/2066126 to your computer and use it in GitHub Desktop.
Facebook OAuth 2.0 Login Tutorial
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
<?php | |
require '../facebook/src/facebook.php'; | |
$facebook = new Facebook(array( | |
'appId' => 'YOUR_APP_ID', | |
'secret' => 'YOUR_APP_SECRET', | |
)); | |
// Get User ID | |
$user = $facebook->getUser(); | |
if ($user) { | |
try { | |
$user_profile = $facebook->api('/me'); | |
} catch (FacebookApiException $e) { | |
error_log($e); | |
$user = null; | |
} | |
} | |
if ($user) { | |
$logoutUrl = $facebook->getLogoutUrl(); | |
} else { | |
$loginUrl = $facebook->getLoginUrl(); | |
} | |
$larry = $facebook->api('/larryrubin'); | |
?> | |
<!doctype html> | |
<html xmlns:fb="http://www.facebook.com/2008/fbml"> | |
<head> | |
<title>Facebook Login with OAuth 2.0 - SocialBlocks</title> | |
<style> | |
body { | |
font-family: 'Lucida Grande', Verdana, Arial, sans-serif; | |
} | |
h1 a { | |
text-decoration: none; | |
color: #3b5998; | |
} | |
h1 a:hover { | |
text-decoration: underline; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>SocialBlocks: Facebook Login with OAuth 2.0</h1> | |
<?php if ($user): ?> | |
<a href="<?php echo $logoutUrl; ?>">Logout</a> | |
<?php else: ?> | |
<div> | |
Login using OAuth 2.0 handled by the PHP SDK: | |
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a> | |
</div> | |
<?php endif ?> | |
<h3>PHP Session</h3> | |
<pre><?php print_r($_SESSION); ?></pre> | |
<?php if ($user): ?> | |
<h3>You</h3> | |
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture"> | |
<h3>Your User Object (/me)</h3> | |
<pre><?php print_r($user_profile); ?></pre> | |
<?php else: ?> | |
<strong><em>You are not Connected.</em></strong> | |
<?php endif ?> | |
<h3>Public profile of Larry</h3> | |
<img src="https://graph.facebook.com/larryrubin/picture"> | |
<?php echo $larry['name']; ?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment