Caf*_*ots 5 php google-app-engine codeigniter oauth google-oauth
嗨,我想从这里学到一些东西,基本上我希望我的系统访问在谷歌帐户,不知何故我设法做以下
但我觉得我在某些方面做错了,这就是 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');
}
}
Run Code Online (Sandbox Code Playgroud)
但这是我的索引控制器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');
}
}
Run Code Online (Sandbox Code Playgroud)
什么是使用MVC PHP的正确过程我需要做ff.:
A.单击按钮sign in to google
B.获取令牌并将其保存到会话
C.将令牌用于我的整个系统(在每个控制器中)
我现在拥有的是A&B,但C i完全不知道该怎么做.
任何人都可以帮我这个.任何建议评论都很受欢迎.提前致谢.
小智 3
您的代码可能有点令人困惑,因为它处理授权 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);
Run Code Online (Sandbox Code Playgroud)
现在,如果您希望它可以跨多个控制器使用,那么 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);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
然后你只需要这样做就可以在你的控制器中使用它:
$this->load->library('someclass');
Run Code Online (Sandbox Code Playgroud)
您还可以创建特定 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);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
然后在你的控制器中使用它:
$this->load->library('someclass');
$this->someclass->analytics();
Run Code Online (Sandbox Code Playgroud)
了解有关 CodeIgniter 库的更多信息:
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
| 归档时间: |
|
| 查看次数: |
3094 次 |
| 最近记录: |