Facebook Connect Codeigniter

时间:2012-03-18 08:48:41

标签: php facebook codeigniter

我尝试在localhost上进行Facebook登录并遇到一些问题。

首先,我使用Codeigniter。为了进行Facebook登录,在库中,我提取了Facebook SDK,其中包括Facebook,base_facebook和fb_ca_chain_bundle.crt。

然后我决定在models目录下创建一个Facebook_Model.php:

<?php
class Facebook_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();

        $config = array(
                        'appId'  => 'XXXXXXXXXXXXXXXXXXXX',
                        'secret' => 'XXXXXXXXXXXXXXXXXXXX',
                        'fileUpload' => true, // Indicates if the CURL based @ syntax for file uploads is enabled.
                        );

        $this->load->library('Facebook', $config);

        $user = $this->facebook->getUser();

        // We may or may not have this data based on whether the user is logged in.
        //
        // If we have a $user id here, it means we know the user is logged into
        // Facebook, but we don't know if the access token is valid. An access
        // token is invalid if the user logged out of Facebook.
        $profile = null;
        if($user)
        {
            try {
                // Proceed knowing you have a logged in user who's authenticated.
                $profile = $this->facebook->api('/me?fields=id,name,link,email');
            } catch (FacebookApiException $e) {
                error_log($e);
                $user = null;
            }
        }

        $fb_data = array(
                        'me' => $profile,
                        'uid' => $user,
                        'loginUrl' => $this->facebook->getLoginUrl(
                            array(
                                'scope' => 'email,user_birthday,publish_stream', // app permissions
                                'redirect_uri' => 'localhost:81/videoyorum/' // URL where you want to redirect your users after a successful login
                            )
                        ),
                        'logoutUrl' => $this->facebook->getLogoutUrl(),
                    );

        $this->session->set_userdata('fb_data', $fb_data);
    }

}

然后我创建了一个控制器,以便使用我的模型:

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Welcome extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Facebook_Model');
        $this->load->helper('url');
    }

    public function index() {
        $this->load->view('welcome_message');
        $fb_data = $this->session->userdata('fb_data');

        print_r($fb_data);


            $data = array(
                'fb_data' => $fb_data,
            );



        $this->load->view('welcome_message', $data);
    }

}

当然是一种观点:

<body>
<div>
  <?php if(!$fb_data['me']): ?>
  Please login with your FB account: <a href="<?php echo $fb_data['loginUrl']; ?>">login</a>
  <!-- Or you can use XFBML -->
  <div class="fb-login-button" data-show-faces="false" data-width="100" data-max-rows="1" data-scope="email,user_birthday,publish_stream"></div>
  <?php else: ?>
  <img src="https://graph.facebook.com/<?php echo $fb_data['uid']; ?>/picture" alt="" class="pic" />
  <p>Hi <?php echo $fb_data['me']['name']; ?>,<br />
    <a href="<?php echo site_url('topsecret'); ?>">You can access the top secret page</a> or <a href="<?php echo $fb_data['logoutUrl']; ?>">logout</a> </p>
  <?php endif; ?>
</div>
</body>

enter image description here

我想知道fb_data会打印什么:

Array ( [me] => [uid] => 0 [loginUrl] => https://www.facebook.com/dialog/oauth?client_id=XXXXXXX&redirect_uri=localhost%3A81%2Fvideoyorum%2F&state=XXXXXXXXXXX&scope=email%2Cuser_birthday%2Cpublish_stream [logoutUrl] => https://www.facebook.com/logout.php?next=http%3A%2F%2Flocalhost%3A81%2Fvideoyorum%2F&access_token=XXXXXXXXX%XXXXXXXXXX)

XXXX部分包含密钥。

我复制了这些信息并将其粘贴到我的浏览器中。它给出了同样的错误。

这些是我的fb应用设置:
enter image description here

出了什么问题?是什么导致错误?

2 个答案:

答案 0 :(得分:0)

尝试设置

localhost:81/videoyorum/
你的$ fb_data_array中的

http://localhost:81/videoyorum

或者,如果这不起作用,请尝试使用非本地服务器(获取网络托管服务提供商)。

答案 1 :(得分:0)

将您的本地主机名更改为域名。因为Facebook应用不支持本地主机地址。
File Path-> C:\WINDOWS\system32\drivers\etc
 这是主机文件
的示例 127.0.0.1 localhost loopback
::1 localhost
127.0.0.1 localhost
127.0.0.1 facebooktest.com
Read this