$ facebook-> getUser()总是返回0
如果没有登录或需要许可,我使用以下代码来验证Facebook使用并将其重定向到登录页面。问题是getUser()总是返回0,导致代码卡在重定向循环中。任何想法?
Am using the following code to authenticate Facebook uses and redirecting them to login page if not logged in or require permission. Problem is getUser() always returns 0 causing the code to get stuck in a redirecting loop. Any ideas?
include_once("facebook.php");
$app_id = 'xxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// initialize facebook
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
echo $user;
if ($user) {
try {
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
else
{
$loginUrl = $facebook->getLoginUrl(array('scope' => 'email'));
header('Location: '.$loginUrl);
}
我只是在使用Facebook API时不添加代码键值。系统需要它才能验证
After days of searching I found that I just wasn't adding a 'code' key value when using the Facebook API. The system needs it in order to validate
当您使用API函数$ facebook-> getLoginUrl();它会带您进入登录页面(当您正确验证时)只需在导航栏中使用code =x参数返回到您自己的网站。所以当实例化Facebook对象时,您只需使用获取请求来获取这些信息。
When you use the API function $facebook->getLoginUrl(); it takes you to a login-page that (when you authenticate properly) simply returns you to your own website with a code="x" parameter in the navigation bar. So when instantiating the Facebook Object you simply use a get request to nab this piece of information.
当您的页面重新加载Facebook API时,请确保其关键值之一是
'code'=> $ _GET ['code'],
sorted ..
when your page reloads the Facebook API, make sure that one of its key values is 'code' => $_GET['code'], sorted..
真的很生气,我不得不把自己弄清楚在这里查看getLoginUrl()文档。 http://developers.facebook。 com / docs / reference / php / facebook-getLoginUrl / ,你会发现它没有指定它返回什么,只是它授权应用程序,无论这意味着什么。
Really angry that I had to figure this out myself.. Look at the getLoginUrl() docs here http://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/ and you'll find that nowhere does it specify what it returns, just that it "authorises" the app, whatever the hell that means.
同样在这里找到的PHP API文档的主页上 http://developers.facebook.com/docs/reference/php/ ,它指出所需的最少参数是应用程序秘密和appID。如果是这样,那么为什么添加代码来排序我的问题?
Also on the main page of the PHP API docs found here http://developers.facebook.com/docs/reference/php/, it states that the minimum number of parameters required are an app secret and an appID. If this is the case, then why does adding the code sort my problem?
如果我说废话,请回复。我想知道我的方式的错误:)。
If I'm talking nonsense, please respond. I like to know the error of my ways :).