php $facebook->getSession() 在示例代码中返回 null。这可以吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2804824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$facebook->getSession() returns null in the example code. is that ok?
提问by mmutilva
Running the example code for the Facebook APII get a null session object, and I should get a non-null object giving the comment in the code. What am I doing wrong?
运行Facebook API的示例代码,我得到一个空会话对象,我应该得到一个非空对象,在代码中给出注释。我究竟做错了什么?
In other words, in my index.php this fragment from the example code shows "no session"when I go to http://apps.facebook.com/my_appin my browser:
换句话说,在我的 index.php 中,当我在浏览器中访问时,示例代码中的这个片段显示“无会话”http://apps.facebook.com/my_app:
<?php
require './facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => '...', // actual value replaced by '...' for this post
'secret' => '...', // actual value replaced by '...' for the post
'cookie' => true,
));
// We may or may not have this data based on a $_GET or $_COOKIE based session.
//
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$session = $facebook->getSession();
if ($session) {
echo "session ok";
}
else {
echo "no session";
}
?>
Note: in my server index.php and facebook.php are in the same folder.
注意:在我的服务器中 index.php 和 facebook.php 在同一个文件夹中。
采纳答案by Gazler
Have you linked your app yet?
您是否已关联您的应用程序?
if ($session) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
<?php if ($me): ?>
<a href="<?php echo $logoutUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
</a>
<?php else: ?>
<div>
Using JavaScript & XFBML: <fb:login-button></fb:login-button>
</div>
<div>
Without using JavaScript & XFBML:
<a href="<?php echo $loginUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">
</a>
</div>
<?php endif ?>
Code taken from the link you provided.
从您提供的链接中获取的代码。
回答by Frank Young
Please check post #8 at http://forum.developers.facebook.net/viewtopic.php?pid=267627
请在http://forum.developers.facebook.net/viewtopic.php?pid=267627查看帖子 #8
It worked for me. Thank you user "standardcombo".
它对我有用。感谢用户“标准组合”。
I have copied it here for your convenience.
为方便起见,我已将其复制到此处。
- Go to your developer page "My Applications"
- Select your app and 'Edit Settings'
- Advanced > Migrations > Enable the "OAuth 2.0 for Canvas (beta)"
- 转到您的开发者页面“我的应用程序”
- 选择您的应用程序和“编辑设置”
- 高级 > 迁移 > 启用“OAuth 2.0 for Canvas(测试版)”
回答by Shawn Hurley
I'm not sure if you've gotten this answered yet, but here's what I found from working through this myself. Even if the user is logged in to facebook, the first time you try to get a session it will be null. The example application I found puts a login button on the form - if you do that, though, then the user will have to click on the button even they're already logged in to facebook. They won't be prompted again, but it's an extra user action.
我不确定你是否已经得到了这个答案,但这是我自己通过这个工作发现的。即使用户登录到 facebook,您第一次尝试获取会话时也会为空。我发现的示例应用程序在表单上放置了一个登录按钮 - 但是,如果您这样做,那么即使用户已经登录到 facebook,也必须单击该按钮。不会再次提示他们,但这是一个额外的用户操作。
So in my app and in what worked for several others I found in the forums is just to redirect to the login URL. (If you inspect the url, one of its parameters is "return_session=1".) When that comes back, then you'll have a session and can proceed normally.
因此,在我的应用程序中以及我在论坛中发现的其他几个应用程序中,只是重定向到登录 URL。(如果您检查 url,它的参数之一是“return_session=1”。)当它返回时,您将有一个会话并且可以正常进行。
But in my application, if I don't have an application token, then I can't get that session either, so I have to get an application token first. To get an application token, look at the excellent description from http://forum.developers.facebook.com/viewtopic.php?id=56789, the post from dynamoman on May 7 2010 (about the fourth post down).
但是在我的应用程序中,如果我没有应用程序令牌,那么我也无法获得该会话,因此我必须先获取应用程序令牌。要获取应用程序令牌,请查看http://forum.developers.facebook.com/viewtopic.php?id=56789中的出色描述,这是 dynamoman 于 2010 年 5 月 7 日发布的帖子(大约第四个帖子)。
One thing I ran into is that regardless of where I tell the authentication is the "next" page, it goes to the page configured in the canvas. So instead of three separate pages, I only have two, and the canvas callback URL has to handle whichever state it's in.
我遇到的一件事是,无论我在哪里告诉身份验证是“下一个”页面,它都会转到画布中配置的页面。因此,我只有两个页面,而不是三个单独的页面,并且画布回调 URL 必须处理它所处的任何状态。
My actual code is inside a framework so it's not directly applicable; as an algorithm, it is:
我的实际代码在一个框架内,所以它不能直接适用;作为一种算法,它是:
landing page:
if the facebook api token is not loaded,
redirect to the authorization url
try to load the user id // to validate that the session
if the user id is not loaded,
redirect to the loginurl from the facebook api
// if it reaches here, then we have both an api token and a session
authorization page:
get authorization token // see the post for how to do that
redirect back to the page configured as the canvas url
There may be a better way, and I'm sure someone more familiar with this than myself could have a better solution, or post generic code.
可能有更好的方法,我相信比我自己更熟悉这个的人可以有更好的解决方案,或者发布通用代码。

