Ejemplo n.º 1
0
<?php

// Start a session and load the OAuth library.
session_start();
require_once __DIR__ . '/oauth-client/src/facebook.class.php';
// Create a new Facebook object.
$facebook = new OAuthFacebook("0000000000000000", "0000000000000000000000000000000000000000", array("session_prefix" => "fb_"));
// Try to fetch an access token with the code in $_GET["code"]. Also check the state in $_GET["state"].
try {
    $facebook->getAccessTokenFromCode("http://example.com/facebook-1/code.php");
    // --------
    // If we got here, no error was thrown and an access token was successfully retrieved from the code.
    // Output the code and access token.
    echo "Success! Click the link at the bottom of the page to return home and fetch data using the access token.<br />\n";
    echo "Code: " . htmlspecialchars($_GET["code"]) . "<br />\n";
    echo "Access Token: " . htmlspecialchars($facebook->accessToken()) . "\n<br /><br />\n\n";
} catch (Exception $error) {
    echo "Error - Click the link at the bottom of the page to return home and try again: " . $error->getMessage() . "\n<br /><br />\n\n";
}
// Output a link to the homepage to fetch data using the access token.
echo "<a href=\"./\">Home</a>\n";
Ejemplo n.º 2
0
<?php

// Start a session and load the OAuth library.
session_start();
require_once __DIR__ . '/oauth-client/src/facebook.class.php';
// Delete the access token if needed.
if (isset($_GET["del_token"]) && isset($_SESSION["fb_token"])) {
    unset($_SESSION["fb_token"]);
}
// Create a new Facebook object.
$facebook = new OAuthFacebook("0000000000000000", "0000000000000000000000000000000000000000", array("session_prefix" => "fb_"));
// Output a Login Button.
echo $facebook->loginButton("Login with Facebook", "https://example.com/facebook-1/code.php", array("email", "user_friends"));
// Try fetching the user's data. If an error is thrown, show a link to the login dialog.
if ($facebook->accessToken() != null) {
    // The user is logged in, you can do whatever you like here.
    // In this example we just print the profile data, along with the profile picture and permissions.
    $profile = $facebook->userProfile();
    echo "<pre>" . print_r($profile, true) . "</pre><br /><br />\n\n";
    // Profile picture
    $profilepicture = $facebook->profilePicture();
    echo "<pre>" . print_r($profilepicture, true) . "</pre><br /><br />\n\n";
    // Permissions
    $permissions = $facebook->permissions();
    echo "<pre>" . print_r($permissions, true) . "</pre><br /><br />\n\n";
    // Friends
    if ($facebook->permission("user_friends")) {
        $request = $facebook->api(OAuth2::GET, "/me/friends");
        $request->execute();
        $friends = $request->responseObject();
    } else {