在MVC PHP中工作的google-oauth库
-
21-12-2019 - |
题
嗨,我想从这里学到一些东西,基本上我希望我的系统的访问在谷歌帐户不知何故,我设法做到以下几点
- 获取客户端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');
}
}
但这是我的索引控制器 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我完全不知道该怎么做。
任何人都可以帮我这个。任何建议评论都是很好的赞赏。先谢谢你。
解决方案
您拥有的代码可能有点混乱,因为它处理授权url,重定向到Google,回调到您的网站,并在同一时间保存访问令牌。
当涉及到使用访问令牌对api执行身份验证调用时,它要简单得多。如果你有一个有效的访问令牌存储在你的会话,你真的可以在任何地方使用它来初始化谷歌应用程序,这种方式:
// 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
不隶属于 StackOverflow