你好,我想从这里学习一些东西,基本上我希望我的系统访问可以通过谷歌账户进行管理。我已经设法完成以下操作:
- 获取客户端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.