PHP facebook SDK 4.0登录错误

时间:2014-05-25 16:13:54

标签: facebook-php-sdk

我在官方网页上寻找文档,但没有任何帮助,所以我构建了这个代码并且它不起作用。

    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}

我还尝试通过隐身模式尝试设置新应用,但总是出现此错误。

2 个答案:

答案 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>';
}

Source

答案 1 :(得分:3)

避免PHP facebook SDK 4.0登录错误的步骤

  1. 如果您使用的是Windows操作系统,请先在PC中安装composer
  2. 下载facebook SDK 4.0并重命名您想要的内容(例如: fbsdk )。
  3. 运行命令提示符并找到最近下载的sdk文件夹。
  4. 检查是否存在composer.json文件
  5. 如果存在composer.json文件,则输入以下命令: composer install否则您已在同一文件夹中创建了新文件composer.json,其代码如下:{ "require" : { "facebook/php-sdk-v4" : "4.0.*" } }
  6. 如果您将在第5行中输入正确的命令,那么您将收到以下消息:使用包信息加载composer存储库安装依赖项(包括require-dev)。
  7. 创建index.php文件并在文件顶部加autoload.php
  8. 例如,按照用于登录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);
    
    }