微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP / Graph API:为什么我的facebook会话不起作用?

我是Facebook Graph API的新开发者.我已经按照本教程:https://www.webniraj.com/2014/05/01/facebook-api-php-sdk-updated-to-v4-0-0.我测试了一次,它的工作原理.我有修改和重新测试,这是工作.但是,第二天,没有…我的facebook会话令牌有变化,我不知道为什么以及如何.

有我的代码

// ALL includes and variables

// start session
session_start();

// init app with app id and secret
FacebookSession::setDefaultApplication( $app_ID, $app_secret );

// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( $url );

// generate login url with scope, each permission as element in array
$loginUrl = $helper->getLoginUrl( array($access_right) );

// 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;
    }
}  

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 );
   }

}

// 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
$graphObject = (new FacebookRequest( $session, 'GET', '/me/accounts'))->execute()->getGraphObject()->asArray();

// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';

$page_token = $graphObject['data'][0]->access_token;

echo '<p>Page token : '.$page_token.'</p>';

$session = new FacebookSession($page_token);

/* make the API call */
$request = new FacebookRequest(
  $session,
  'POST',
  '/'.$pageID.'/Feed',
  array (
    'message' => 'This is a test message',
  )
);
$response = $request->execute();
$graphObject = $response->getGraphObject();

/* handle the result */
// 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>';
}

我的目标只是在我的粉丝页面添加一条消息,它第一次运行.为什么变量“$_SESSION [‘fb_token’]”现在为NULL?我怎么能让它发挥作用?

谢谢你的回复

编辑1:
我发现我的会话已将CSRF标题更改为:’fb_token’为’FBRLH_state’.关于这种变化的想法?

解决方法:

这是我使用最新的Facebook PHP-SDK 4.0.9登录用户文件内容(我认为).

<?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/GraphUser.PHP' );

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\GraphUser;

if (!isset($_SESSION['facebookUserId']) || !isset($_SESSION['facebookSession']) || !isset($_SESSION['facebookUserProfile'])) {
    // init app with app id (APPID) and secret (SECRET)
    FacebookSession::setDefaultApplication($appId,$appSecret);

    // login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper( $canvasUrl );
    try {
         $FBSession = $helper->getSessionFromredirect();
    } catch( FacebookRequestException $ex ) {
        // When Facebook returns an error
    } catch( Exception $ex ) {
         // When validation fails or other local issues
    }
    if (!isset($FBSession)) {
        // STEP ONE - REDIRECT THE USER TO FACEBOOK FOR AUTO LOGIN / APP APPRoval
        header('Location: ' . $helper->getLoginUrl());
        exit();
    } else {
        $user_profile = (new FacebookRequest($FBSession, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
        $_SESSION['facebookUserId'] = $user_profile->getID();
        $_SESSION['facebookSession'] = $FBSession; // I DON'T THINK THIS ACTAULLY WORKS RIGHT
        $_SESSION['facebookUserProfile'] = $user_profile;

    }
}

我只是包含这个文件,如下所示.

include ('SimpleFacebookLogin.PHP');

我尝试保存会话,它在会话中没有正确保存,所以我想我也错过了一些东西,但我可以在创建会话后成功登录.我不知道如何再次访问它.我希望这有帮助.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐