Facebook Session将js-SDK转移到php-SDK [英] Facebook Session Transfer js-SDK to php-SDK

查看:174
本文介绍了Facebook Session将js-SDK转移到php-SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP SDK 2.1.2,并使用标准的 http://connect.facebook。 net / en_US / all.js 作为JS SDK。

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

这是我想要实现的:

1)初始化JS Sdk,使用(email,user_location,user_birthday)调用Fb.UI,方法:'oauth'作为响应,如果用户成功授予权限,可以执行ajax调用服务器然后使用PHP-SDK来调用Graph API并获取所需的数据。 (嗯,你可能会争论通过JS SDK调用graph API,但目前的情况需要我使用PHP SDK)

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)

问题

通过ajax调用的文件不会初始化会话,而是取消null,而是从理想的情况下加载该cookie并且有权限的活动会话已经在客户端授予。

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.

我的代码

主文件

<?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>

通过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";
 ?>

如果您在上述示例中看到,我应该在alert框中取得整个/ me对象我刚刚得到你好。我不知道为什么这不工作?我缺少什么?

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?

场景: -
如果我在页面重新加载(top.location.href)后,授予我获得所需的对象,但我们正在尝试避免重新加载此机制。我也试图设置所需的P3P头,因为我以为可能会有一些cookie问题,但仍然无法加载。

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.

此外,我已经尝试过这个PHP SDK 2.1.1,2.1.2,3.0.1,我听说过较新的PHP-SDK尚不兼容JS SdK,是否真的?

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?

谢谢

推荐答案

您无法使用AJAX调用设置Cookie:在域B上设置第三方Cookie域A要求域B上的页面采取域A上的页面,您的ajax调用不在。

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.

我建议尝试使用您从响应oauth对话框( permObj-> access_token 获取访问令牌的cookie,除非我错了,我可以)并发布作为AJAX调用的请求参数。然后,您可以使用令牌值调用 $ facebook-> setAccessToken 方法,您应该全部设置。

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.

这篇关于Facebook Session将js-SDK转移到php-SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆