Facebook SDK v4 $ session无效

时间:2015-03-03 18:22:13

标签: php facebook

您好我在我的网站上使用facebook SDKv4登录facebook 在对应用程序进行身份验证后,用户无法登录到我的站点,也没有将详细信息保存到数据库中,并且在身份验证后我可以看到登录按钮

<?php
session_start();
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
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/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );

use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest; 
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;

// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('APPID','SECRET');


// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'example.com' );


/***************************************************session handinling*********************************************/

// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
  // create new session from saved access_token

//$_SESSION['fb_token'] = $session->getToken();
  // create a session using saved token or the new one we generated at login
 // $session = new FacebookSession( $session->getToken() );
 $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;
  }
}  

if ( !isset( $session ) || $session === null ) {
  // no session exists

  try {
    $session = $helper->getSessionFromRedirect();
  } catch( FacebookRequestException $ex ) {
    // When Facebook returns an error
    // handle this better in production code
    print_r( $ex );
  } catch( Exception $ex ) {
    // When validation fails or other local issues
    // handle this better in production code
    print_r( $ex );
  }

}


/*****************************************************session handling*****************************************/

try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}

// see if we have a session
if ( isset( $session ) ) {
  // graph api request for user data
 $_SESSION['fb_token'] = $session->getToken();
  // create a session using saved token or the new one we generated at login
  $session = new FacebookSession( $session->getToken() );
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject();
$fuid= $graphObject->getProperty('id');              // To Get Facebook ID
$fname=$graphObject->getProperty('first_name'); // To Get Facebook full name
$lname=$graphObject->getProperty('last_name'); // To Get Facebook full name
$femail=$graphObject->getProperty('email'); 
$gender=$graphObject->getProperty('gender'); 

CODE TO SAVE DETAILS TO DB


/*************************************************************post on users walll******************************************************/
// Publish to User’s Timeline
$request = ( new FacebookRequest( $session, 'POST', '/me/feed', array(
  'message' => 'Only TEXT Goes Here'
) ) )->execute();

// Get response as an array, returns ID of post
$response = $request->getGraphObject()->asArray();

print_r( $response );

// Graph API to publish to timeline with additional parameters
$request = ( new FacebookRequest( $session, 'POST', '/me/feed', array(
    'name' => 'Name',
    'caption' => 'CAtion',
    'link' => 'example.com',
    'message' => 'msg'
) ) )->execute();

// Get response as an array, returns ID of post
$response = $request->getGraphObject()->asArray();

/****************************************************************post on users wall************************************************/


redirect($site_url);





  //echo  print_r( $graphObject, 1 );
} else {
  // show login url
$permissions = ['email', 'user_likes','publish_actions'];
  echo '<a href="' . $helper->getLoginUrl($permissions) . '"><img src="'.$img_cdn.'fbcon.png"></a>';
}

&GT;

任何人都可以告诉我哪里做错了吗?

0 个答案:

没有答案