用户通过$ facebook-> getLoginUrl()时没有启动访问令牌或会话;

时间:2012-02-10 14:40:42

标签: php facebook-php-sdk codeigniter-2

我已经从Facebook运行了php sdk示例并且它在我的服务器上运行良好但是当我将代码放入codeigniters控制器的方法时它根本没有返回会话 - 有没有人经历过这个?

即使认为它已经过身份验证并且用户已经接受了我无法恢复会话的应用程序 - 会话中唯一的内容就是

[fb_32567534414789_state] => 3d34472df552ad7fef1911d2f97122f0

甚至url参数都是从Facebook传回的 - 状态和代码都有值。

问题是它在一个独立的例子中运行良好且没有问题但是当我将它带入控制器方法时它不起作用。

我已经注释掉了我的控制器构造函数及其方法中的每一行。然后只用模型方法列出示例代码,从数据库中提取api密钥/秘密;它不起作用;

我最近升级到最新的sdk - 我被困住了,不知道该怎么做。

更新

我在我的服务器上添加了两个示例,以显示其操作方式

这是一个全新的codeigniter安装的控制器方法中的标准sdk示例(从github中提取最新的sdk)

https://beta.exceleratedsolutions.com/testfb/welcome/testfb/

这是标准的sdk示例

https://beta.exceleratedsolutions.com/testfb/examples/example.php

代码列表

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * NOTICE OF LICENSE
 * 
 * Licensed under the Academic Free License version 3.0
 * 
 * This source file is subject to the Academic Free License (AFL 3.0) that is
 * bundled with this package in the files license_afl.txt / license_afl.rst.
 * It is also available through the world wide web at this URL:
 * http://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to obtain it
 * through the world wide web, please send an email to
 * licensing@ellislab.com so we can send you a copy immediately.
 *
 * @package     CodeIgniter
 * @author      EllisLab Dev Team
 * @copyright   Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
 * @license     http://opensource.org/licenses/AFL-3.0 Academic Free License (AFL 3.0)
 * @link        http://codeigniter.com
 * @since       Version 1.0
 * @filesource
 */

class Welcome extends CI_Controller {
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->view('welcome_message');
    }

    public function testfb(){

        $this->load->helper('facebook');

        // Create our Application instance (replace this with your appId and secret).
        $facebook = new Facebook(array(
          'appId'  => '325675344144789',
          'secret' => 'xxxxxxxxxxx',
        ));

        // Get User ID
        $user = $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.

        if ($user) {
          try {
            // Proceed knowing you have a logged in user who's authenticated.
            $user_profile = $facebook->api('/me');
          } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
          }
        }

        // Login or logout url will be needed depending on current user state.
        if ($user) {
          $logoutUrl = $facebook->getLogoutUrl();
        } else {
          $loginUrl = $facebook->getLoginUrl();
        }

        // This call will always work since we are fetching public data.
        $naitik = $facebook->api('/naitik');

        ?>
        <!doctype html>
        <html xmlns:fb="http://www.facebook.com/2008/fbml">
          <head>
            <title>php-sdk</title>
            <style>
              body {
                font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
              }
              h1 a {
                text-decoration: none;
                color: #3b5998;
              }
              h1 a:hover {
                text-decoration: underline;
              }
            </style>
          </head>
          <body>
            <h1>php-sdk</h1>

            <?php if ($user): ?>
              <a href="<?php echo $logoutUrl; ?>">Logout</a>
            <?php else: ?>
              <div>
                Login using OAuth 2.0 handled by the PHP SDK:
                <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
              </div>
            <?php endif ?>

            <h3>PHP Session</h3>
            <pre><?php print_r($_SESSION); ?></pre>

            <?php if ($user): ?>
              <h3>You</h3>
              <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

              <h3>Your User Object (/me)</h3>
              <pre><?php print_r($user_profile); ?></pre>
            <?php else: ?>
              <strong><em>You are not Connected.</em></strong>
            <?php endif ?>

            <h3>Public profile of Naitik</h3>
            <img src="https://graph.facebook.com/naitik/picture">
            <?php echo $naitik['name']; ?>
          </body>
        </html>

        <?php
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
?>

如你所见 - 这只是标准

我已将此问题推向了具有相同问题的pagodabox

2 个答案:

答案 0 :(得分:0)

可能是因为如果您正在使用某种会话管理,那么您已在代码中的其他位置启动了会话。

答案 1 :(得分:-1)

您可以尝试将Facebook php-sdk放入库文件夹而不是帮助程序。此外,您可能会按照本教程,它适用于我,也许您没有设置一些所需的配置(步骤1到5),还检查5月3日更新的教程结束:

http://www.dannyherran.com/2011/02/facebook-php-sdk-and-codeigniter-for-basic-user-authentication/

此致