1
votes

I am creating a login function for my website, using the FB login API.

I use https://graph.facebook.com/oauth/access_token?client_id=ID&redirect_uri=URL&client_secret=SECRET&code=CODE

I am able to retrieve name and basics. But email I can not get.

In my app, I have set permissisions, including "email". But still, when the APP says "WEBSITE wants access to your information and friend list" the email option is not mentioned.

What am I doing wrong?

Best regards, Rasmus

1
“In my app, I have set permissisions” – these settings are for automatic login triggered when access the app from the app center only. Whenever you are handling login yourself, you have to ask for permissions via the scope parameter … - CBroe
Fantastic. But now my page does not redirect my to my URL but to the domain/index.php? - Rasmus Bang Olesen
Well then you have done something wrong maybe. - CBroe

1 Answers

1
votes

You must include the scope parameter and the redirect parameter.

$config = array();
$config['appId'] = 'xxx';
$config['secret'] = 'xxx';

$facebook = new Facebook($config);

if(isset($_GET['act']) && $_GET['act'] == "logout") {
    $facebook->destroySession();
}


$user = $facebook->getUser();
echo $user;
if ($user) {

    try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
    }
    echo "<pre>";
    print_r($user_profile);
    echo "</pre>";  
    $logout = $facebook->getLogoutUrl();

    //echo $logout;

    echo "<a href='test.php?act=logout'>Logout</a>";


} else {

    $login = $facebook->getLoginUrl(array("scope"=>"email","display"=>"popup","redirect_uri"=>"http://domain.com/test.php"));

    echo "<a href='".$login."'>Login</a>";

}