简体   繁体   中英

Why am I getting a “the page isn't redirecting properly” error?

I got this from nettuts, can someone please tell me why am I getting a "the page isn't redirecting properly" error?

<?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);
}

You redirect authenticated user to $facebook->getLoginUrl(array(...)) , creating a redirect loop.

You should redirect only unauthenticated users (redirect with req_perms should be in else clause). The redirect in try should happen only if you detect that user hasn't granted you all required permissions.

You can check granted permissions by invoking:

$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()
));


Modified code:

 <?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 

If you are using the latest PHP SDK, several changes have been made and the permissions you are asking do not work that way.

Here is an updated code.

<?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 

More info: http://developers.facebook.com/docs/authentication/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM