本文介绍了google-oauth库在MVC PHP中的会话工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想从这里学习一些东西,基本上我想要我的系统的访问是在谷歌的帐户在某种程度上我设法做以下




  • 获取客户端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');
}
}

但这是我的索引控制器登录它只有按钮使用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.点击按钮登录google

B.获取令牌并将其保存到会话

C.将令牌用于我的整个系统(在每个控制器中)



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



任何人都可以帮助我这个。任何建议的评论是赞赏。提前感谢。

解决方案

您可能有些困惑,因为它处理授权网址,重定向到Google,回调您的网站,并同时保存访问令牌。



当使用访问令牌对api执行身份验证的调用,它更简单。如果您在会话中存储了有效的访问令牌,则可以在任何位置使用它来初始化Google应用,方法如下:

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

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

现在,如果你想让它在多个控制器上可用,CodeIgniter最合适的方法是创建一个包含上述代码的新库。



使用库的优点是它们可以在CodeIgniter配置中自动加载,并且可以在代码的任何地方轻松访问



示例库:

 <?php if defined('BASEPATH'))exit('不允许直接脚本访问); 

class Someclass {

private $ client;

public function __construct()
{
...

//客户端
$ 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('不允许直接脚本访问); 

class Someclass {

private $ client;

public function __construct()
{
...

//客户端
$ 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库的更多信息:




Hi I would like to learn something from here, basically I want the access of my system to be in google account somehow I manage to do the following

  • get client id, redirect uris, secret keys
  • authentication from google account
  • get token

but I i felt I was doing wrong all in some part, this is the 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.

解决方案

The code you have might be a bit confusing because it handles authorization url, redirect to Google, callback to your site, and save the access token at the same time.

When it comes to use the access token to perform authenticated call to the api, it's much simpler. If you have a valid access token stored in you session, you can really use it anywhere to initialize the Google Application, this way:

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

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

Now if you want this to be available across several controllers, the most appropriate way in CodeIgniter would be to create a new library wrapping the code above.

The advantage of using a library is that they can be auto-loaded in CodeIgniter config, and are easily accessible anywhere in your code.

Example library:

<?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);
    }
}

?>

Then you just have to do this to use it in your controller:

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

You could also create shortcuts to specific APIs. For example, if you wanted a quick access to Google Analytics API, you could do this:

<?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);
    }
}

?>

And then use it this way in your controller:

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

Learn more about CodeIgniter libraries:

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

这篇关于google-oauth库在MVC PHP中的会话工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:22