Facebook redirect with extended permissions
I am trying to work out facebook connect based on this tutorial: http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/
They have given you a few scripts to use to figure it out, this is the extended permissions one:
<?php
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getSession();
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);
}
When I execute this it lets me allow the extended permissions but then I get a redirect loop error. When I check in facebook it has granted extended permissions.
Now I tried to just implement it into the login script they gave, which is this
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getSession();
if(!empty($session)) {
# Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
try{
$uid = $facebook->getUser();
$user = $facebook->api('/me');
} catch (Exception $e){}
if(!empty($user)){
# We have an active session, let's check if we have already registered the user
$query = mysql_query("SELECT * FROM users WHERE oauth_prov = 1 AND oauth_id = ". $user['id']);
$result = mysql_fetch_array($query);
# If not, let's add it to the database
echo $user['id'] . $user['name'];
if(empty($result)){
$query = mysql_query("INSERT INTO users (oauth_prov, oauth_id, username) VALUES ('1', {$user['id']}, '{$user['n开发者_高级运维ame']}')");
$query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
$result = mysql_fetch_array($query);
}
// this sets variables in the session
$_SESSION['id'] = $result['id'];
$_SESSION['oauth_uid'] = $result['oauth_id'];
$_SESSION['oauth_provider'] = $result['oauth_provider'];
$_SESSION['username'] = $result['username'];
} else {
# For testing purposes, if there was an error, let's kill the script
die("There was an error.");
}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
However, I don't know where it should go, as I would like to request permissions at the same time as the login to get the users details. If I put this:
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){}
After I set the session values then I don't even get to allow anything, it just hangs and I get a redirect error. If I put it just after getting the facebook user_id then it gives me the allow dialog (although one after the other as its obviously asking twice, something I would like to stop) but then again gives me a redirect error, having granted the extended permissions
I really don't have a clue where to put it, the documentation for facebook is absolutely abysmal
Instead of this:
# 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'));
try:
$url = $facebook->getLoginUrl(array('scope' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'));
for an complete example see the php-sdk: https://github.com/facebook/php-sdk/blob/master/examples/example.php
精彩评论