Facebook 会话将 js-SDK 传输到 php-SDK
我正在使用 PHP SDK 2.1.2 并使用标准 http://connect.facebook.net/en_US /all.js 作为 JS SDK。
这就是我想要实现的:
1)初始化JS Sdk,使用(email,user_location,user_birthday)调用Fb.UI,方法:'oauth',作为响应,如果用户成功授予权限,则执行对服务器进行 ajax 调用,然后使用 PHP-SDK 调用 Graph API 并获取所需的数据。 (好吧,你可能会争论通过 JS SDK 调用图形 API,但当前场景要求我使用 PHP SDK)
问题
通过 ajax 调用的文件不会初始化会话,并且我get null,相反,facebook sdk 理想情况下应该加载 cookie 并具有活动会话,因为已经在客户端授予了权限。
我的代码
主文件
<?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";
?>
如果您在上面的示例中看到,我应该在警报框中获取整个 /me 对象,而不是只得到“hello”。我不知道为什么这不起作用?我缺少什么?
场景:- 如果我在授予权限后重新加载页面(top.location.href),我会得到所需的对象,但我们试图避免使用此机制重新加载。我还尝试设置所需的 P3P 标头,因为我认为可能存在一些 cookie 问题,但它仍然无法加载。
另外,我已经尝试过使用 PHP SDK 2.1.1、2.1.2、3.0.1,我听说较新的 PHP-SDK 尚未与 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用 AJAX 调用设置 cookie:域 A 在域 B 上设置第三方 cookie 要求域 B 上的页面处于获取域 A 上页面的行为,而您的 ajax 调用并未执行此操作。
我建议不要尝试使用 cookie,而是从对 oauth 对话框的响应中获取访问令牌(
permObj->access_token
除非我错了,而且我可能是错的)并发布作为 AJAX 调用的请求参数。然后,您可以使用令牌值调用$facebook->setAccessToken
方法,这样就一切就绪了。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.