google oauth库在MVC PHP会话中工作

google oauth库在MVC PHP会话中工作,php,codeigniter,google-app-engine,oauth,google-oauth,Php,Codeigniter,Google App Engine,Oauth,Google Oauth,嗨,我想从这里学到一些东西,基本上我希望我的系统的访问权限在谷歌帐户不知何故,我设法做到以下几点 获取客户端id、重定向URI、密钥 来自google帐户的身份验证 领取代币 但我觉得我在某些方面做错了,这是Oauth2callback class Oauth2callback extends CI_Controller { function __construct(){ parent::__construct(); $this->load

嗨,我想从这里学到一些东西,基本上我希望我的系统的访问权限在谷歌帐户不知何故,我设法做到以下几点

  • 获取客户端id、重定向URI、密钥
  • 来自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');
        }
    }
但这是我的索引控制器,
登录
它只有按钮
使用谷歌登录
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.点击按钮
登录谷歌

B.获取令牌并将其保存到会话
C.将令牌用于我的整个系统(在每个控制器中)

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


谁能帮我一下吗。任何建议和评论都将不胜感激。提前感谢。

您的代码可能有点混乱,因为它处理授权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);
    }
}

?>
您还可以创建特定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);
    }
}

?>
了解有关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);
    }

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

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