google-oauth库在MVC PHP中进行会话

时间:2014-04-14 02:45:26

标签: php codeigniter google-app-engine oauth google-oauth

您好,我想从这里学到一些东西,基本上我想让我的系统访问谷歌帐户以某种方式我设法做以下

  • 获取客户端ID,重定向uris,密钥
  • 来自Google帐户的身份验证
  • 获取令牌

但我觉得我在某些方面做错了,这是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');
        }
    }

但这是我的索引控制器Login它只有按钮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');


    }
}


使用MVC PHP的正确过程是什么我需要做ff。 :
A.单击按钮sign in to google
B.获取令牌并将其保存到会话中 C.将令牌用于我的整个系统(在每个控制器中)

我现在所拥有的是A& B但C我完全不知道该怎么做。

任何人都可以帮助我。任何建议评论都很受欢迎。提前谢谢。

1 个答案:

答案 0 :(得分:3)

您拥有的代码可能会有点混乱,因为它处理授权网址,重定向到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 AnalyticsAPI,可以执行以下操作:

<?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