dpda53918 2015-07-30 23:26
浏览 164

Facebook javascript登录PHP SDK

I am using the FB facebook-php-sdk-v4-5.0-dev.zip API files.

I'm attempting to make the Facebook javascript and php SDKs work together. I have FB.login working find, I can make Graph requests, etc. Now I want to pass off the access token to my PHP code, and continue from there. I am initializing FB in javascript:

window.fbAsyncInit = function() {
    FB.init({
        appId      : appId, //my appId
        cookie     : true,  // enable cookies for session access
        xfbml      : true,  // parse social plugins on this page
        version    : 'v2.2' // use version 2.2
    });

And now that FB in initialized, I do some other work using the API, after which I push out to my php code with:

window.location.replace(<my url>.php);

Inside my PHP application, I have the following:

$fb = new Facebook\Facebook([ 'app_id' => <my app_id>,
                              'app_secret' => <my app_secret>
                              'default_graph_version' => 'v2.2',
                            ]);

$jsHelper = $fb->getJavaScriptHelper();
try 
{
    $accessToken = $jsHelper->getAccessToken();
} 
catch(Facebook\Exceptions\FacebookResponseException $e) 
{
    // When Graph returns an error
    print('Graph returned an error: ' . $e->getMessage() );
    exit;
}
catch(Facebook\Exceptions\FacebookSDKException $e) 
{
    // When validation fails or other local issues
    print( 'Facebook SDK returned an error: ' . $e->getMessage() );
    exit;
}

if (isset($accessToken)) 
{
    print("Logged in already");
}
else
{
    print("Not logged in");
}

What I see is "Not logged in". From what I can tell, no cookie is generated (I dumped out $_COOKIE at the top of my PHP file). I have also monitored the network calls, and can see no cookie coming down from Facebook on my oauth requests... which is where I would assume that such a cookie would come from.

Thanks, Andy

  • 写回答

2条回答 默认 最新

  • douzai9405 2015-07-31 06:36
    关注

    I'd recommend that you update to the v5.0.0 of the PHP SDK, or is there a specific reason you're using this outdated one?

    Furthermore, have a look at the example at

    Copied sample code:

    # /js-login.php
    $fb = new Facebook\Facebook([
      'app_id' => '{app-id}',
      'app_secret' => '{app-secret}',
      'default_graph_version' => 'v2.2',
      ]);
    
    $helper = $fb->getJavaScriptHelper();
    
    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)) {
      echo 'No cookie set or no OAuth data could be obtained from cookie.';
      exit;
    }
    
    // Logged in
    echo '<h3>Access Token</h3>';
    var_dump($accessToken->getValue());
    
    $_SESSION['fb_access_token'] = (string) $accessToken;
    
    // User is logged in!
    // You can redirect them to a members-only page.
    //header('Location: https://example.com/members.php');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?