我在官方网页上寻找文档,但没有任何帮助,所以我构建了这个代码并且它不起作用。
FacebookSession::setDefaultApplication('app-id', 'app-secret');
$helper = new \Facebook\FacebookRedirectLoginHelper('http://my-return-url');
if (isset($_SESSION['token'])) {
$session = new FacebookSession($_SESSION['token']);
if (!$session->validate()) {
$session = null;
}
} else {
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
die("Oops! :/");
} catch(Exception $ex) {
die($ex->message());
}
}
if (isset($session)) {
$_SESSION['token'] = $session->getToken();
$session = new FacebookSession($session->getToken());
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();
echo '<pre>';
print_r($graphObject);
echo '</pre>';
} else {
echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}
我认为问题出在SDK中,但我不确定。我总是得到这个错误:
致命错误:未捕获的异常&#39; Facebook \ FacebookSDKException&#39;消息&#39;会话已过期,或对此应用无效。&#39;在/var/www/html/Facebook/FacebookSession.php:238堆栈跟踪:#0 /var/www/html/Facebook/FacebookSession.php(216):Facebook \ FacebookSession :: validateSessionInfo(Object(Facebook \ GraphSessionInfo), &#39; my-app-id&#39;,&#39; app-secret ...&#39;)#1 /var/www/html/verify.php(98):Facebook \ FacebookSession-&gt;第238行/var/www/html/Facebook/FacebookSession.php中的validate()#2 {main}
我还尝试通过隐身模式尝试设置新应用,但总是出现此错误。
答案 0 :(得分:6)
我同意,FB文件非常糟糕。此外,他们经常更改他们的SDK版本,几天前他们发布了v 4.0.5。试试这个 - &gt;
<?php
// include required files form Facebook SDK
// added in v4.0.5
require_once( 'Facebook/FacebookHttpable.php' );
require_once( 'Facebook/FacebookCurl.php' );
require_once( 'Facebook/FacebookCurlHttpClient.php' );
// added in v4.0.0
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
// added in v4.0.5
use Facebook\FacebookHttpable;
use Facebook\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
// added in v4.0.0
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxx','yyy' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://yourwebsite.com/app/' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
} else {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
echo $ex->message;
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo '<a href="' . $helper->getLogoutUrl( $session, 'http://yourwebsite.com/app/logout.php' ) . '">Logout</a>';
} else {
// show login url
echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends' ) ) . '">Login</a>';
}
答案 1 :(得分:3)
避免PHP facebook SDK 4.0登录错误的步骤
composer.json
文件,则输入以下命令:
composer install
否则您已在同一文件夹中创建了新文件composer.json
,其代码如下:{
"require" : {
"facebook/php-sdk-v4" : "4.0.*"
}
}
index.php
文件并在文件顶部加autoload.php
。例如,按照用于登录facebook的示例代码
session_start();
require_once('/home3/users/public_html/fbsdk/vendor/autoload.php');
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
FacebookSession::setDefaultApplication(YOUR APP_ID, YOUR SECRET_KEY);
$helper = new FacebookRedirectLoginHelper('http://example.com/fbsdk/login.php');
if(isset($_SESSION['access_token'])) {
$access_token = $_SESSION['access_token'];
$session = new FacebookSession($access_token);
} else {
unset($_SESSION['access_token']);
try {
$session = $helper->getSessionFromRedirect();
if($session)
$_SESSION['access_token'] = $session->getToken();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(Exception $ex) {
// When validation fails or other local issues
}
}
if ($session) {
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graphObject = $response->getGraphObject();
header('Location: ./home.php');
} else {
$loginUrl = $helper->getLoginUrl();
header('Location: ' . $loginUrl);
}