Cakephp 如何在Cake PHP框架下集成Google登录
我一直在尝试将Google登录集成到我的一个使用CakePHP框架开发的网站上。到目前为止,我这样做是不成功的。有人知道怎么做吗?您使用的是哪个版本的CakePHP 但是,您应该查看该线程: 关于CakePHP/提供者身份验证也有很好的资源:Cakephp 如何在Cake PHP框架下集成Google登录,cakephp,login,oauth,cakephp-1.3,Cakephp,Login,Oauth,Cakephp 1.3,我一直在尝试将Google登录集成到我的一个使用CakePHP框架开发的网站上。到目前为止,我这样做是不成功的。有人知道怎么做吗?您使用的是哪个版本的CakePHP 但是,您应该查看该线程: 关于CakePHP/提供者身份验证也有很好的资源: Hiii…请循序渐进 第一步:首先,你应该从github或../app/Vendor目录下载Google文件。。。。 确保Google文件包括以下文件 Auth文件夹、缓存文件夹、contrib文件夹、外部文件夹、io文件夹、服务文件夹和auto
CREATE TABLE IF NOT EXISTS `users` (<br>
`id` int(11) NOT NULL AUTO_INCREMENT,<br>
`first_name` varchar(60) DEFAULT NULL,<br>
`last_name` varchar(60) DEFAULT NULL,<br>
`email` varchar(80) DEFAULT NULL,<br>
`password` varchar(64) DEFAULT NULL,<br>
`social_id` varchar(45) DEFAULT NULL,<br>
`picture` varchar(100) DEFAULT NULL,<br>
`gender` char(1) DEFAULT NULL,<br>
`created` datetime DEFAULT NULL,<br>
`updated` datetime DEFAULT NULL,<br>
`uuid` varchar(70) DEFAULT NULL,<br>
`status` int(11) DEFAULT NULL,<br>
PRIMARY KEY (`id`),<br>
KEY `email_idx` (`email`)<br>
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
第八步:创建谷歌控制器。复制下面的代码
App::import('Vendor', 'google', array('file' => 'google/config.php'));<br>
App::import('Vendor', 'google', array('file' => 'google/Google_Client.php'));<br>
App::import('Vendor', 'google', array('file' => 'google/contrib/Google_PlusService.php'));<br>
App::import('Vendor', 'google', array('file' => 'google/contrib/Google_Oauth2Service.php'));<br>
class GoogleController extends AppController {<br>
public $components = array('Paginator', 'Session');<br>
public $uses = array('User');<br>
public $layout = '';<br>
public function google_login() {
$this->autoRender = false;
require_once '../Config/google_login.php';
$client = new Google_Client();
$client->setScopes(array('https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/plus.me'));
$client->setApprovalPrompt('auto');
$plus = new Google_PlusService($client);
$oauth2 = new Google_Oauth2Service($client);
if (isset($_GET['code'])) {
$client->authenticate(); // Authenticate
$_SESSION['access_token'] = $client->getAccessToken(); // get the access token here<br>
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
$_SESSION['access_token'] = $client->getAccessToken();
$user = $oauth2->userinfo->get();
try {
if (!empty($user)) {
$check = $this->User->find('first', array('conditions' => array('email' => $user['email'], 'status !=' => 'Trash')));
if (empty($check)) {
$data = array();
$data['email'] = $user['email'];
$data['name'] = $user['given_name'];
$data['user_name'] = $this->random_username($user['given_name']);
$this->User->save($data);
$id = $this->User->getLastInsertID();
$currentuser = $this->User->find('first', array('conditions' => array('user_id' => $id, 'status !=' => 'Trash')));
$this->Session->write('User', $currentuser['User']);
$this->Session->write('Userlogin', TRUE);
$this->Session->setFlash('Logged in Successfully !', '', array(''), 'front_success');
return $this->redirect(array('action' => 'profile', 'controller' => 'users'));
} else {
$this->Session->write('User', $check['User']);
$this->Session->write('Userlogin', TRUE);
$this->Session->setFlash('Logged in Successfully !', '', array(''), 'front_success');
return $this->redirect(array('action' => 'profile', 'controller' => 'users'));
}
}
} catch (Exception $e) {
$this->Session->setFlash(GOOGLE_LOGIN_FAILURE, 'default', array('class' => 'message error'), 'error');
$this->redirect(BASE_PATH . 'login');
}
}
}
}
App::import('Vendor','google',array('file'=>'google/config.php')
App::import('Vendor','google',array('file'=>'google/google_Client.php')
App::import('Vendor','google',array('file'=>'google/contrib/google_PlusService.php')
App::import('Vendor','google',array('file'=>'google/contrib/google_Oauth2Service.php')
类GoogleController扩展AppController{
public$components=数组('Paginator','Session');
public$uses=array('User');
公共$layout='';
公共函数google_login(){
$this->autoRender=false;
需要_once'../Config/google_login.php';
$client=新的Google_客户端();
$client->setScopes(阵列)https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/plus.me'));
$client->setApprovalPrompt('auto');
$plus=新的谷歌plus服务($client);
$oauth2=新的Google_oauth2服务($client);
如果(isset($_GET['code'])){
$client->authenticate();//验证
$\u会话['access\u token']=$client->getAccessToken();//在此处获取访问令牌
}
如果(isset($_会话['access_token'])){
$client->setAccessToken($_会话['access_token']);
}
如果($client->getAccessToken()){
$\会话['access\u token']=$client->getAccessToken();
$user=$oauth2->userinfo->get();
试一试{
如果(!empty($user)){
$check=$this->User->find('first',array('conditions'=>array('email'=>$User['email'],'status!='='=>Trash'));
如果(空($支票)){
$data=array();
$data['email']=$user['email'];
$data['name']=$user['given_name'];
$data['user_name']=$this->random_username($user['given_name']);
$this->User->save($data);
$id=$this->User->getLastInsertID();
$currentuser=$this->User->find('first',array('conditions'=>array('User\u id'=>$id',status!='=>'Trash'));
$this->Session->write('User',$currentuser['User']);
$this->Session->write('Userlogin',TRUE);
$this->Session->setFlash('Logged Successfully!','',数组('','front_success');
返回$this->redirect(数组('action'=>'profile','controller'=>'users');
}否则{
$this->Session->write('User',$check['User']);
$this->Session->write('Userlogin',TRUE);
$this->Session->setFlash('Logged Successfully!','',数组('','front_success');
返回$this->redirect(数组('action'=>'profile','controller'=>'users');
}
}
}捕获(例外$e){
$this->Session->setFlash(谷歌登录失败,'default',数组('class'=>'messageerror'),'error');
$this->重定向(基本路径。'login');
}
}
}
}
步骤9:在app/Vendor/google/config.php文件中,您可以看到应用程序名称、oauth2_客户机id、oauth2_客户机密钥、oauth2_重定向_uri。所以你必须给出你的细节。示例:'application_name' => 'YOUR APPLICATION NAME',<br>
'oauth2_client_id' => 'YOUR CLIENT ID',<br>
'oauth2_client_secret' => 'YOUR SECRET KEY',<br>
'oauth2_redirect_uri' => 'https://your site/google/google_login', <br>
“应用程序名称”=>“您的应用程序名称”,
“oauth2_client_id”=>“您的客户端id”,
“oauth2\u客户端\u密钥”=>“您的密钥”,
'oauth2\u重定向\u uri'=>'https://your site/google/google\u login',
就这样!:)
我面临的问题是,我在facebook开发者中错误地提到了重定向URI&在Config.php文件中。因此,您应该在bootstrap.php文件、config.php文件、href=”“link&开发者站点中提供相同的重定向路径
希望它能帮助。。。谢谢。谢谢您的回复,我一直在使用1.3版。虽然这是一组非常丰富的资源,而且最终非常有用,但根据SO的答案指南,这一建议充其量只是一个链接“答案”,即它是一个评论,而不是答案。这是为注释设计的类型响应的完美示例。此外,像这样的Q,不符合Q的SO设计处理类型的标准,应该关闭,并通过注释而不是答案部分进行回答。这个问题现在已经很老了,但是如果您正在寻找CakePHP 3 Google登录的完整教程,请看这里的“请编辑以符合SO指南”,这需要正确的语法和拼写(并删除help/please/Thank/etc cruft)。易读性受到阻碍,帖子质量低下,当遗漏单词、句子不完整以及故意拼写错误时,沟通也会受到阻碍。它很懒。代码已经够难了,没有用自定义快捷语言编写的句子。有关详细信息,请参阅章节
<?php<
require_once '../Vendor/Google/src/config.php';
require_once '../Vendor/Google/src/Google_Client.php';
require_once '../Vendor/Google/src/contrib/Google_PlusService.php';
require_once '../Vendor/Google/src/contrib/Google_Oauth2Service.php';
href="https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=YOUR REDIRECT URI&client_id=YOUR CLIENT ID.apps.googleusercontent.com&scope=email+profile&access_type=online&approval_prompt=auto"
App::import('Vendor', 'google', array('file' => 'google/config.php'));<br>
App::import('Vendor', 'google', array('file' => 'google/Google_Client.php'));<br>
App::import('Vendor', 'google', array('file' => 'google/contrib/Google_PlusService.php'));<br>
App::import('Vendor', 'google', array('file' => 'google/contrib/Google_Oauth2Service.php'));<br>
class GoogleController extends AppController {<br>
public $components = array('Paginator', 'Session');<br>
public $uses = array('User');<br>
public $layout = '';<br>
public function google_login() {
$this->autoRender = false;
require_once '../Config/google_login.php';
$client = new Google_Client();
$client->setScopes(array('https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/plus.me'));
$client->setApprovalPrompt('auto');
$plus = new Google_PlusService($client);
$oauth2 = new Google_Oauth2Service($client);
if (isset($_GET['code'])) {
$client->authenticate(); // Authenticate
$_SESSION['access_token'] = $client->getAccessToken(); // get the access token here<br>
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
$_SESSION['access_token'] = $client->getAccessToken();
$user = $oauth2->userinfo->get();
try {
if (!empty($user)) {
$check = $this->User->find('first', array('conditions' => array('email' => $user['email'], 'status !=' => 'Trash')));
if (empty($check)) {
$data = array();
$data['email'] = $user['email'];
$data['name'] = $user['given_name'];
$data['user_name'] = $this->random_username($user['given_name']);
$this->User->save($data);
$id = $this->User->getLastInsertID();
$currentuser = $this->User->find('first', array('conditions' => array('user_id' => $id, 'status !=' => 'Trash')));
$this->Session->write('User', $currentuser['User']);
$this->Session->write('Userlogin', TRUE);
$this->Session->setFlash('Logged in Successfully !', '', array(''), 'front_success');
return $this->redirect(array('action' => 'profile', 'controller' => 'users'));
} else {
$this->Session->write('User', $check['User']);
$this->Session->write('Userlogin', TRUE);
$this->Session->setFlash('Logged in Successfully !', '', array(''), 'front_success');
return $this->redirect(array('action' => 'profile', 'controller' => 'users'));
}
}
} catch (Exception $e) {
$this->Session->setFlash(GOOGLE_LOGIN_FAILURE, 'default', array('class' => 'message error'), 'error');
$this->redirect(BASE_PATH . 'login');
}
}
}
}
'application_name' => 'YOUR APPLICATION NAME',<br>
'oauth2_client_id' => 'YOUR CLIENT ID',<br>
'oauth2_client_secret' => 'YOUR SECRET KEY',<br>
'oauth2_redirect_uri' => 'https://your site/google/google_login', <br>