Google OAuth库在MVC PHP中如何使用session?

5

你好,我想从这里学习一些东西,基本上我希望我的系统访问可以通过谷歌账户进行管理。我已经设法完成以下操作:

  • 获取客户端ID、重定向URI和密钥
  • 通过谷歌账户进行身份验证
  • 获取令牌

但是我感觉在某些部分做错了,这就是Oauth2callback

class Oauth2callback extends CI_Controller {


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

        $this->load->helper('url');
        $this->load->library('session');
        require_once APPPATH.'libraries/Google/Client.php';
        session_start();


    }

    public function index()
    {


        $client_id = $this->config->item('client_id');
        $client_secret = $this->config->item('client_secret');
        $redirect_uri = $this->config->item('redirect_uri');

        $client = new Google_Client();
        $client->setClientId($client_id);
        $client->setClientSecret($client_secret);
        $client->setRedirectUri($redirect_uri);
        $client->addScope("https://www.googleapis.com/auth/userinfo.email");
        $client->addScope("https://www.googleapis.com/auth/userinfo.profile");

        if (isset($_GET['code'])) {
            $client->authenticate($_GET['code']);
            $_SESSION['access_token'] = $client->getAccessToken();
            $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

            header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
        }

        if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
            $client->setAccessToken($_SESSION['access_token']);
        } else {
            $authUrl = $client->createAuthUrl();
        }

        if ($client->getAccessToken()) {
            $_SESSION['access_token'] = $client->getAccessToken();


        }


        if(isset($authUrl)) {
            header('location:'. base_url());
        }else{
            header('location:'. base_url().'dashboard');
        }
    }

but this is my index controller the Login it only has button sign in with Google

class Login extends CI_Controller {


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

        $this->load->helper('url');
        $this->load->library('session');
        require_once APPPATH.'libraries/Google/Client.php';
        session_start();
    }

    public function index()
    {
        //$this->checkSession();
        $client_id = $this->config->item('client_id');
        $client_secret = $this->config->item('client_secret');
        $redirect_uri = $this->config->item('redirect_uri');

        $client = new Google_Client();
        $client->setClientId($client_id);
        $client->setClientSecret($client_secret);
        $client->setRedirectUri($redirect_uri);
        $client->addScope("https://www.googleapis.com/auth/userinfo.email");
        $client->addScope("https://www.googleapis.com/auth/userinfo.profile");

            $authUrl = $this->data['authUrl'] = $client->createAuthUrl();
            $this->load->view('login/index.php',$this->data);
            $this->load->view('template/pre_footer');
            $this->load->view('template/footer');


    }
}


what would be the right process of this using MVC PHP I need to do the ff. :
A. click button sign in to google
B. get token and save it to session
C. used the token to my entire system (in every controller)

what I have right now is A & B but the C i totally don't know what to do.

could anyone help me with this. any suggestion comment is well appreciated. thanks in advance.


你有找到解决方案吗?我也遇到了同样的问题,请帮帮我,谢谢。 - nick
1个回答

3
您所拥有的代码可能有些混乱,因为它同时处理授权URL、重定向到Google、回调到您的站点并保存访问令牌。
当使用访问令牌执行针对API的身份验证调用时,这更加简单。如果您在会话中存储了有效的访问令牌,则可以在任何地方使用它来初始化Google应用程序,像这样:
// client
$client = new Google_Client();
$client->setApplicationName('Google Application');
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);

// token
$client->setAccessToken($token);

现在,如果你想让这个功能在多个控制器中可用,在CodeIgniter中最适合的方法是创建一个包装上面代码的新库。
使用库的优点是它们可以在CodeIgniter配置中自动加载,并且可以在您的代码中轻松访问任何位置。
示例库:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Someclass {

    private $client;

    public function __construct()
    {
        ... 

        // client
        $client = new Google_Client();
        $this->client->setApplicationName('Google Application');
        $this->client->setClientId($clientId);
        $this->client->setClientSecret($clientSecret);
        $this->client->setRedirectUri($redirectUri);

        // token
        $this->client->setAccessToken($token);
    }
}

?>

然后您只需在控制器中执行以下操作即可使用它:
 $this->load->library('someclass');

您也可以创建特定API的快捷方式。例如,如果您想快速访问Google Analytics API,可以这样做:

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

class Someclass {

    private $client;

    public function __construct()
    {
        ... 

        // client
        $client = new Google_Client();
        $this->client->setApplicationName('Google Application');
        $this->client->setClientId($clientId);
        $this->client->setClientSecret($clientSecret);
        $this->client->setRedirectUri($redirectUri);

        // token
        $this->client->setAccessToken($token);
    }

    public function analytics()
    {
        return new Google_Service_Analytics($this->client);
    }
}

?>

然后在您的控制器中这样使用它:

 $this->load->library('someclass');
 $this->someclass->analytics();

了解更多关于CodeIgniter库的内容:

http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接