-2

API 呼び出しと Facebook からのログインのコードを次に示します。問題は、Facebook にリダイレクトするときにサインインしないことです。コードのどの部分が間違っていますか?どうすれば修正できますか?

require 'facebook/facebook.php';
require 'config/fbconfig.php';
require 'config/functions.php';
$url = $_REQUEST['lasturl'];

$facebook = new Facebook(array(
   'appId' => APP_ID,
          'secret' => APP_SECRET,
          'cookie' => true
    ));

$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)) {
        # User info ok? Let's print it (Here we will be adding the login and registering routines)
       // echo '<pre>';
        //print_r($user);
       // echo '</pre><br/>';
        $username = $user['name'];
        $user = new User();
        $userdata = $user->checkUser($uid, 'facebook', $username);
        if(!empty($userdata)){
            session_start();
            $_SESSION['id'] = $userdata['id'];
            $_SESSION['oauth_id'] = $uid;

            $_SESSION['username'] = $userdata['username'];
            $_SESSION['oauth_provider'] = $userdata['oauth_provider'];
            header("Location: ".$url);
        }
    } 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);
}
4

2 に答える 2

0

以下のコードを試してください:$session = $facebook->getSession();動作しません

 $facebook = new Facebook(array(
       'appId' => APP_ID,
              'secret' => APP_SECRET,
              'cookie' => true
        ));

     $uid = $facebook->getUser();

    if ($uid) {
        # Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
    try {

        $user = $facebook->api('/me');
    } catch (Exception $e) {


    }

        if ($uid) {
            # User info ok? Let's print it (Here we will be adding the login and registering routines)
           // echo '<pre>';
            //print_r($user);
           // echo '</pre><br/>';
            $username = $user['name'];
            $user = new User();
            $userdata = $user->checkUser($uid, 'facebook', $username);
            if(!empty($userdata)){
                session_start();
                $_SESSION['id'] = $userdata['id'];
                $_SESSION['oauth_id'] = $uid;

                $_SESSION['username'] = $userdata['username'];
                $_SESSION['oauth_provider'] = $userdata['oauth_provider'];
                header("Location: ".$url);
            }
        } 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);
    }
于 2012-10-30T12:53:03.613 に答える
0

実際、コードは問題なく、ログイン資格情報の取得は正常に機能するはずです。しかし、問題はここにあります。

if (!empty($user)) 

API呼び出しでユーザー情報を取得する前に、これを最初に置くべきだと思います。関数 getUser が ID を返す場合、ユーザー情報を取得するためだけに現在のセッションを取得する必要はありません。

于 2012-10-30T12:42:44.427 に答える