使用带有codeigniter的google api将google登录用户数据存储到会话中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用带有codeigniter的google api将google登录用户数据存储到会话中相关的知识,希望对你有一定的参考价值。
我正在尝试使用goignapi使用codeigniter实现登录。它已经正常登录。登录后它将重定向到另一个页面。但我想要的是登录后将数据存储到会话中并使用另一个视图。然后重定向到重定向重定向URL。但它一旦登录重定向到重定向url。当我尝试将数据存储到会话中时,它不会去那里。我是新手。请有人帮我谢谢。
<?php
class HomeController extends CI_controller{
public function __construct(){
parent::__construct();
$this->load->model('Login_Model');
require_once APPPATH.'third_party/src/Google_Client.php';
require_once APPPATH.'third_party/src/contrib/Google_Oauth2Service.php';
}
public function glogin(){
$clientId = '390448461087-ivmu5fq7nl67gt8qd09vu6ljh27eh6fg.apps.googleusercontent.com'; //Google client ID
$clientSecret = '1qaO8Sa98zqSJU2J2xRFvsW9'; //Google client secret
$redirectURL = base_url() .'Vendorcontroller/index/';
//Call Google API
$gClient = new Google_Client();
$gClient->setApplicationName('Login with google');
$gClient->setClientId($clientId);
$gClient->setClientSecret($clientSecret);
$gClient->setRedirectUri($redirectURL);
$google_oauthV2 = new Google_Oauth2Service($gClient);
if(isset($_GET['code']))
{
$gClient->authenticate($_GET['code']);
$_SESSION['token'] = $gClient->getAccessToken();
header('Location: ' . filter_var($redirectURL, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token']))
{
$gClient->setAccessToken($_SESSION['token']);
}
if ($gClient->getAccessToken()) {
$userData=$google_oauthV2->userinfo->get();
//when i try to save session data like below it is not coming here
$_SESSION['id']=$userData['id'];
$_SESSION['email']=$userData['email'];
$_SESSION['familyName']=$userData['familyName'];
$_SESSION['gender']=$userData['gender'];
$_SESSION['age']=$userData['age'];
$_SESSION['picture']=$userData['picture'];
$_SESSION['givenName']=$userData['givenName'];
echo "<pre>";
print_r($userData);
die;
}
else
{
$url = $gClient->createAuthUrl();
header("Location: $url");
exit;
}
}
}
答案
是的我已经改变了它为我工作的redirecturl
<?php
class HomeController extends CI_controller{
public function __construct(){
parent::__construct();
$this->load->model('Login_Model');
require_once APPPATH.'third_party/src/Google_Client.php';
require_once APPPATH.'third_party/src/contrib/Google_Oauth2Service.php';
}
public function glogin(){
$clientId = '390448461087-ivmu5fq7nl67gt8qd09vu6ljh27eh6fg.apps.googleusercontent.com'; //Google client ID
$clientSecret = '1qaO8Sa98zqSJU2J2xRFvsW9'; //Google client secret
$redirectURL = base_url() .'HomeController/glogin/';
//Call Google API
$gClient = new Google_Client();
$gClient->setApplicationName('Login with google');
$gClient->setClientId($clientId);
$gClient->setClientSecret($clientSecret);
$gClient->setRedirectUri($redirectURL);
$google_oauthV2 = new Google_Oauth2Service($gClient);
if(isset($_GET['code']))
{
$gClient->authenticate($_GET['code']);
$_SESSION['token'] = $gClient->getAccessToken();
header('Location: ' . filter_var($redirectURL, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token']))
{
$gClient->setAccessToken($_SESSION['token']);
}
if ($gClient->getAccessToken()) {
$userData=$google_oauthV2->userinfo->get();
//when i try to save session data like below it is not coming here
$_SESSION['id']=$userData['id'];
$_SESSION['email']=$userData['email'];
$_SESSION['familyName']=$userData['familyName'];
$_SESSION['gender']=$userData['gender'];
$_SESSION['age']=$userData['age'];
$_SESSION['picture']=$userData['picture'];
$_SESSION['givenName']=$userData['givenName'];
echo "<pre>";
print_r($userData);
die;
}
else
{
$url = $gClient->createAuthUrl();
header("Location: $url");
exit;
}
}
}
以上是关于使用带有codeigniter的google api将google登录用户数据存储到会话中的主要内容,如果未能解决你的问题,请参考以下文章
google-apps 域验证异常的 Codeigniter .htaccess 文件配置
由于 ?gclid=,Google Adwords 出现 Codeigniter 404 错误
Google Adwords 和 CodeIgniter GET 变量回调 URL