【问题标题】:Hybridigniter Auth library and social pluginHybridigniter Auth 库和社交插件
【发布时间】:2013-12-28 02:50:42
【问题描述】:

我已经集成了用于社交登录的 codeigniter 混合身份验证库。我已经为 facebook、google 和 twitter 创建了开发者密钥。当我尝试使用这些 api 登录时,出现错误。我在 localhost 中运行我的应用程序。将 url 重定向到 localhost 时是否有问题。

Facebook 身份验证:

用户已取消身份验证或提供商拒绝连接。

谷歌认证:

错误:redirect_uri_mismatch

推特认证:

用户已取消身份验证或提供商拒绝连接。

控制器(hauth):

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

class HAuth extends CI_Controller {

    public function index()
    {
        $this->load->view('hauth/home');
    }

    public function login($provider)
    {
        log_message('debug', "controllers.HAuth.login($provider) called");

        try
        {
            log_message('debug', 'controllers.HAuth.login: loading HybridAuthLib');
            $this->load->library('HybridAuthLib');

            if ($this->hybridauthlib->providerEnabled($provider))
            {
                log_message('debug', "controllers.HAuth.login: service $provider enabled, trying to authenticate.");
                $service = $this->hybridauthlib->authenticate($provider);

                if ($service->isUserConnected())
                {
                    
                    log_message('debug', 'controller.HAuth.login: user authenticated.');

                    $user_profile = $service->getUserProfile();

                    log_message('info', 'controllers.HAuth.login: user profile:'.PHP_EOL.print_r($user_profile, TRUE));

                    $data['user_profile'] = $user_profile;

                    $this->load->view('hauth/done',$data);
                }
                else // Cannot authenticate user
                {
                    show_error('Cannot authenticate user');
                }
            }
            else // This service is not enabled.
            {
                log_message('error', 'controllers.HAuth.login: This provider is not enabled ('.$provider.')');
                show_404($_SERVER['REQUEST_URI']);
            }
        }
        catch(Exception $e)
        {
            $error = 'Unexpected error';
            switch($e->getCode())
            {
                case 0 : $error = 'Unspecified error.'; break;
                case 1 : $error = 'Hybriauth configuration error.'; break;
                case 2 : $error = 'Provider not properly configured.'; break;
                case 3 : $error = 'Unknown or disabled provider.'; break;
                case 4 : $error = 'Missing provider application credentials.'; break;
                case 5 : log_message('debug', 'controllers.HAuth.login: Authentification failed. The user has canceled the authentication or the provider refused the connection.');
                         //redirect();
                         if (isset($service))
                         {
                            log_message('debug', 'controllers.HAuth.login: logging out from service.');
                            $service->logout();
                         }
                         show_error('User has cancelled the authentication or the provider refused the connection.');
                         break;
                case 6 : $error = 'User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again.';
                         break;
                case 7 : $error = 'User not connected to the provider.';
                         break;
            }

            if (isset($service))
            {
                $service->logout();
            }

            log_message('error', 'controllers.HAuth.login: '.$error);
            show_error('Error authenticating user.');
        }
    }

    public function endpoint()
    {

        log_message('debug', 'controllers.HAuth.endpoint called.');
        log_message('info', 'controllers.HAuth.endpoint: $_REQUEST: '.print_r($_REQUEST, TRUE));

        if ($_SERVER['REQUEST_METHOD'] === 'GET')
        {
            log_message('debug', 'controllers.HAuth.endpoint: the request method is GET, copying REQUEST array into GET array.');
            $_GET = $_REQUEST;
        }

        log_message('debug', 'controllers.HAuth.endpoint: loading the original HybridAuth endpoint script.');
        require_once APPPATH.'/third_party/hybridauth/index.php';

    }
}

/* End of file hauth.php */
/* Location: ./application/controllers/hauth.php */

【问题讨论】:

    标签: codeigniter authentication hybridauth


    【解决方案1】:

    Facebook 错误发生在我身上。我的应用程序已获得授权,但授权后 Facebook 未正确重定向。转到 Facebook>设置>应用程序并检查您的应用程序是否属于授权应用程序。如果是这样,请将其删除并重试。

    【讨论】:

      【解决方案2】:

      您可能会在本地主机上运行 Facebook 和其他第三方身份验证时遇到问题,这可能是由于不同的原因。您可以尝试的一种可能的解决方案是设置一个虚拟域来访问您的 CI 网站。 http://www.kristengrote.com/blog/articles/how-to-set-up-virtual-hosts-using-wamp 给出了在 WAMP 上设置虚拟主机的非常清晰的说明。然后进行相应的 Facebook 应用设置。

      您可能需要检查本地主机上的相关端口是否打开,否则您可能会在接收回认证时遇到问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-09
        • 1970-01-01
        • 2012-08-16
        • 2016-04-21
        • 2012-12-04
        • 1970-01-01
        • 2015-02-01
        相关资源
        最近更新 更多