Facebook Session Transfer js-SDK to php-SDK

北慕城南 提交于 2020-01-03 04:59:19

问题


I am using PHP SDK 2.1.2 and using the standard http://connect.facebook.net/en_US/all.js as the JS SDK.

This is what i want to achieve:

1) Initialize JS Sdk, Call Fb.UI with (email,user_location,user_birthday), method: 'oauth', in response, if the user successfully gives permission do an ajax call to the server and then use PHP-SDK to do a call to Graph API and get the required data. (Well, you might argue to do a call to graph API via JS SDK but the current scenario requires me to use PHP SDK)

The Problem

The file which is called via ajax does not initialize the session and i get null, instead the facebook sdk should ideally load the cookies and have an active session since the permission is granted on the client side already.

My Code

Main File

<?php
session_start();
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
require 'src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
    'appId'  => 'xxxxxxxxxxxxxxxxxxxxxxxx',
    'secret' => 'yyyyyyyyyyyyyyyyyyyyyyyy',
    'cookie' => true,
));

$session = $facebook->getSession();

$me = null;
// Session based API call.
if ($session) {
   try {
       $uid = $facebook->getUser();
       $me = $facebook->api('/me');
       print_r($me);
   } catch (FacebookApiException $e) {
                error_log($e);
   }
 }

?>

<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
    <title>php-sdk</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">         
    </script>
</head>
<body>

<input type="button" onclick="getpermission();"/>
<!--
  We use the JS SDK to provide a richer user experience. For more info,
  look here: http://github.com/facebook/connect-js
-->
<div id="fb-root"></div>
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
  FB.init({
      appId   : '<?php echo $facebook->getAppId(); ?>',
      session : <?php echo json_encode($session); ?>, // don't refetch the session when   PHP already has it
      status  : true, // check login status
      cookie  : true, // enable cookies to allow the server to access the session
      xfbml   : true // parse XFBML
    });

function getpermission(){
      FB.ui({
        method: 'oauth',
        client_id: <?php echo $facebook->getAppId(); ?>,
        perms: 'email,user_location,user_birthday'
        },function(permObj) {
            if(permObj){
                $.ajax({
                        type: "POST",
                        url: 'page2.php',
                        dataType:'text',
                        success: function(data) {

                                return;
                                        },
                        error:function (xhr, ajaxOptions, thrownError){
                            alert(xhr.status);
                            alert(thrownError);
                        }    
                      });

            }
            });
        }

</script>
</body>

The File called via AJAX

<?php
session_start();
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
require 'src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId'  => 'xxxxxxxxxxxxxxxx',
'secret' => 'yyyyyyyyyyyyyyyyyyyyyyyyyyyy',
'cookie' => true,
));

$session = $facebook->getSession();

$me = null;
// Session based API call.
if ($session) {
    try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
        print_r($me);
    } catch (FacebookApiException $e) {
          error_log($e);
    }
 }

 echo "hello";
 ?>

If you see in the above example, i should get the whole /me object in the alert box instead i just get "hello". I don't know why this isn't working? What am i missing?

Scenario:- If i do a page reload (top.location.href) after permissions are granted i get the desired objects but we are trying to avoid a reload with this mechanism. I have also tried to set the desired P3P headers because i thought there might be some cookie issue but it still fails to load.

Also, i have tried this with PHP SDK 2.1.1, 2.1.2, 3.0.1, i have heard somewhere that the newer PHP-SDKs are not yet compatible with JS SdK, is it true?

Thanks in advance.


回答1:


You can't set the cookie using an AJAX call: setting a third-party cookie on domain B by domain A requires that a page on domain B be in the act of fetching a page on domain A, which your ajax call is not doing.

I'd suggest that instead of trying to use the cookie you fetch the access token from the response to the oauth dialog (permObj->access_token unless I'm wrong, and I could be) and post that as a request parameter with your AJAX call. You can then call the $facebook->setAccessToken method with the token value and you should be all set.



来源:https://stackoverflow.com/questions/6460531/facebook-session-transfer-js-sdk-to-php-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!