无法通过Facebook登录 [英] Cannot login through Facebook

查看:159
本文介绍了无法通过Facebook登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是API调用的code和通过Facebook登录。
问题是,当它重定向到Facebook的,它不登录。
哪一部分我的code是错误的,我怎么能解决这个问题?

 要求的Facebook / facebook.php';
需要配置/ fbconfig.php';
需要'配置/的functions.php';
$ URL = $ _REQUEST ['lasturl'];$ Facebook的=新的Facebook(阵列(
   APPID=> APP_ID,
          '秘密'=> APP_SECRET,
          '饼干'=>真正
    ));$会议= $ facebook->的getSession();如果(!空($会话)){
    #活动会话,让我们尝试获取用户ID(的getUser())和用户信息(API-GT&('/我'))
尝试{
    $ UID = $ facebook->的getUser();
    $ USER = $ facebook-> API('/我');
}赶上(例外$ E){
}    如果(!空($用户)){
        #用户信息好吗?让我们来进行打印(在这里,我们将增加在登录和注册程序)
       //回声'< pre>';
        //的print_r($用户);
       //回声'< / pre>< BR />';
        $用户名= $用户['名'];
        $用户=新用户();
        $用户数据= $用户自> checkUser($ UID,Facebook的',$用户名);
        如果(!空($用户数据)){
            在session_start();
            $ _SESSION ['身份证'] = $用户数据['身份证'];
            $ _SESSION ['oauth_id'] = $ UID;            $ _SESSION [用户名] = $用户数据[用户名];
            $ _SESSION ['oauth_provider'] = $用户数据['oauth_provider'];
            标题(位置:$网址);
        }
    }其他{
        #出于测试目的,如果有错误,让我们杀的脚本
        死(有错误。);
    }
}其他{
    #有没有活动的会话,让我们生成一个
    $ LOGIN_URL = $ facebook-> getLoginUrl();
    标题(位置:$ LOGIN_URL);
}


解决方案

尝试下面code: $会议= $ facebook->的getSession(); 唐'T工作

  $ Facebook的=新的Facebook(阵列(
       APPID=> APP_ID,
              '秘密'=> APP_SECRET,
              '饼干'=>真正
        ));     $ UID = $ facebook->的getUser();    如果($ UID){
        #活动会话,让我们尝试获取用户ID(的getUser())和用户信息(API-GT&('/我'))
    尝试{        $ USER = $ facebook-> API('/我');
    }赶上(例外$ E){
    }        如果($ UID){
            #用户信息好吗?让我们来进行打印(在这里,我们将增加在登录和注册程序)
           //回声'< pre>';
            //的print_r($用户);
           //回声'< / pre>< BR />';
            $用户名= $用户['名'];
            $用户=新用户();
            $用户数据= $用户自> checkUser($ UID,Facebook的',$用户名);
            如果(!空($用户数据)){
                在session_start();
                $ _SESSION ['身份证'] = $用户数据['身份证'];
                $ _SESSION ['oauth_id'] = $ UID;                $ _SESSION [用户名] = $用户数据[用户名];
                $ _SESSION ['oauth_provider'] = $用户数据['oauth_provider'];
                标题(位置:$网址);
            }
        }其他{
            #出于测试目的,如果有错误,让我们杀的脚本
            死(有错误。);
        }
    }其他{
        #有没有活动的会话,让我们生成一个
        $ LOGIN_URL = $ facebook-> getLoginUrl();
        标题(位置:$ LOGIN_URL);
    }

Here is the code of API calls and login through Facebook. The problem is that when it redirects to Facebook, it does not sign in. What part of my code is wrong and how can I fix it?

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

解决方案

Try below code : $session = $facebook->getSession(); don't work

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

这篇关于无法通过Facebook登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆