我是从nettuts得到的,有人可以告诉我为什么我得到“页面没有正确重定向”错误?
<?php
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'APP_ID_HERE',
'secret' => 'APP_SECRET_HERE',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getUser();
if(!empty($session)) {
# Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
try{
$uid = $facebook->getUser();
# req_perms is a comma separated list of the permissions needed
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'
));
header("Location: $url");
} catch (Exception $e){}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
答案 0 :(得分:4)
您将经过身份验证的用户重定向到$facebook->getLoginUrl(array(...))
,从而创建了重定向循环。
您应该只重定向未经身份验证的用户(req_perms
重定向应该在else
子句中。仅当您检测到用户未授予您所有必需权限时,try
中的重定向才会发生。
您可以通过调用:
来检查授予的权限$perms = $facebook->api(array(
'method' => 'fql.query',
'query' => 'SELECT email,user_birthday,status_update,publish_stream,user_photos,user_videos FROM permissions WHERE uid=' . $facebook->getUser()
));
<小时/> 修改后的代码:
<?php
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'APP_ID_HERE',
'secret' => 'APP_SECRET_HERE',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getUser();
if(empty($session)) {
# There's no active session, let's generate one
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'
));
header("Location: $url");
exit;
}
// user is logged in
答案 1 :(得分:0)
如果您使用的是最新的PHP SDK,则会进行一些更改,并且您要求的权限不会以这种方式工作。
这是更新的代码。
<?php
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'APP_ID_HERE',
'secret' => 'APP_SECRET_HERE',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getUser();
if(empty($session)) {
# There's no active session, let's generate one
$url = $facebook->getLoginUrl(array(
"response_type"=>"token", //Can also be "code" if you need to
"scope" => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos' ,
"redirect_uri"=> "http://test.com" //Your app callback url
));
header("Location: $url");
exit;
}
// user is logged in