你需要做這樣的事情。這不是我認爲的唯一方式,但對我有用。
的index.php
<?php
require_once __DIR__ . '\..\src\Facebook\autoload.php';
session_start();
$fb = new Facebook\Facebook([
'app_id' => '???',
'app_secret' => '???',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email','user_friends', 'publish_actions', 'user_posts']; //here you set all the permisions
// (be aware that for some permisions you'll need to send your app to Facebook Review)
$loginUrl = $helper->getLoginUrl('http://localhost/fb-callback.php', $permissions);
echo htmlspecialchars($loginUrl); // this will echo the URL that needs to be clicked (something like 'Login with Facebook' button)
?>
FB-callback.php
<?php
require_once __DIR__ . '\..\src\Facebook\autoload.php';
session_start();
$fb = new Facebook\Facebook([
'app_id' => '???', // Replace {app-id} with your app id
'app_secret' => '???',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId('???'); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (! $accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
//header('Location: https://example.com/members.php');
header('Location: http://localhost/page_after_login');
?>
EDIT:該代碼對應於實登錄示例從這裏:https://developers.facebook.com/docs/php/howto/example_facebook_login
是這一個新文件?如果是的話它會在哪裏被保存? –
是的是一個新文件。您可以將其保存在您想要的任何位置,然後修改$ loginUrl的URL。請閱讀本文(對我來說非常有幫助):https://developers.facebook.com/docs/php/howto/example_facebook_login – mitr00
我已經完成了你所說的所有事情,但我似乎遇到了某種錯誤。我做錯了什麼? 警告:require_once(/home/u742400531/public_html/1353http://friends.esy.es/1353/autoload.php):無法打開流:/ home/u742400531/public_html/1353/index中沒有這樣的文件或目錄.php on line 3 致命錯誤:require_once():無法打開所需'/home/u742400531/public_html/1353http://friends.esy.es/1353/autoload.php'(include_path ='。:/ opt/php-5.5/pear')在/home/u742400531/public_html/1353/index.php在線3 –