Store the google loggedin userdata into session using googleapi with codeigniter - php

I am trying to implement login with googleapi using codeigniter.It is logged in fine.After login it is redirecting to another page.But what i want is after login store the data into session and use another view.then redirect to redirect redirect url.But it is once it is login redirect to redirect url. when i try to store the data into session it is not going there.I am new to this.Please somebody help me thanks in Advance.
<?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;
}
}
}

Yes I have change the redirecturl it is worked for me
<?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;
}
}
}

Related

How to style for <a href> for php

<?php
echo "<a href='".$client->createAuthUrl()."'><button>Login with Google</button></a>";
?>
Im trying to add class for <a href> inside php echo , but i cant do any style what i want to do
<?php
echo "<a href='".$client->createAuthUrl()."'><button>Login with Google</button></a>";
?>
You can try something like this, I hope it will help you.
Login with Google"
Your use of single quotes and double quotes is a little mixed up. I should also add that buttons inside a tags aren't really correct. I'd suggest removing the button and styling the a tag instead. With that in mind, here are the options:
Regular CSS method
echo '<a class="mylink" href="'.$client->createAuthUrl().'">Login with Google</a>';
then in the stylesheet:
a.mylink{
styles here
}
Inline styling Method
echo '<a style="styles here" href="'.$client->createAuthUrl().'">Login with Google</a>';
From a google development stand point im going to ask why are you doing it like that? Just redirect the user using the location header if you can detect they aren't logged in.
index.php
login
login.php
$Auth = new Oauth2Authentication();
$client = $Auth->GetClient();
$client = $Auth->initializeClient($client);
relevant code from my Oauth2Authentication class.
function initializeClient($client)
{
try {
// Set the refresh token on the client.
if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
$client->refreshToken($_SESSION['refresh_token']);
}
// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// Set the access token on the client.
$client->setAccessToken($_SESSION['access_token']);
// Refresh the access token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken());
$_SESSION['access_token'] = $client->getAccessToken();
}
return $client;
} else {
// We do not have access request access.
header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
}
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
oauthcallback.php
<?php
require_once __DIR__. '/../vendor/autoload.php';
require_once __DIR__ . '/Oauth2Authentication.php';
// Start a session to persist credentials.
session_start();
// Handle authorization flow from the server.
if (!isset($_GET['code'])) {
// Request authorization of the user by displaying the consent screen
$Auth = new Oauth2Authentication();
$client = $Auth->GetClient();
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
// Exchange the Authorization code for an access token and refresh token.
$Auth = new Oauth2Authentication();
$client = $Auth->GetClient();
$client->fetchAccessTokenWithAuthCode($_GET['code']); // Exchange the authentication code for a refresh token and access token.
// Add access token and refresh token to session.
$_SESSION['access_token'] = $client->getAccessToken();
$_SESSION['refresh_token'] = $client->getRefreshToken();
//Redirect back to main script
$redirect_uri = str_replace("oauth2callback.php","login.php", $client->getRedirectUri());
//header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
header('Location: http://localhost');
}

Google API with PHP calling google_client object

I'm using codeigniter 3.x and php 5.5+. I've been trying to get this to work with no luck. I want to call methods inside a youtube controller class I created that will do different things based on communicating with the google or youtube. The problem is with the Google_Client I believe or it's with how to store $client variable maybe so you can use it in second function. When I try calling this from another function it gives a not login required error even thought the user already did the google verification. How can I tell my second function that the user has already done the authorization and got the token. Everything seems to work very well when both functions are in the same function(I mean just using one function). Also I don't want to verify user on every function or method I execute.
So after first function is done, I get redirected with an access token in the url to the second function but then I get an error Message: Error calling GET https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date=2014-01-01&end-date=2016-01-20&metrics=views&filters=video%3D%3DpC900o6JaMc: (401) Login Required
First function in the controller Youtubeverify:
class Youtubeverify extends CI_Controller{
public function youtube_consent(){
$this->load->library('google');
$OAUTH2_CLIENT_ID = 'MY_ID';
$OAUTH2_CLIENT_SECRET = 'MY_KEY';
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes(array('https://www.googleapis.com/auth/yt-analytics-monetary.readonly','https://www.googleapis.com/auth/youtube.readonly'));
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . "/ci/youtubeverify/display_report",
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
$youtubeReporting = new Google_Service_YouTubeReporting($client);
if (isset($_REQUEST['logout'])) {
$this->session->unset_userdata('youtube_token');
}
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$youtube_token = $client->getAccessToken();
//trying to store the access token in a session
$newdata = array('youtube_token'=>$youtube_token);
$this->session->set_userdata($newdata);
header('Location: ' . $redirect);
}
if ($this->session->userdata('youtube_token')) {
$client->setAccessToken($this->session->userdata('youtube_token'));
}
if ($client->getAccessToken()) {
$client->getAccessToken();
}
else {
// If the user hasn't authorized the app, initiate the OAuth flow
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;
$authUrl = $client->createAuthUrl();
redirect($authUrl);
}
}
Second function in the controller Youtubeverify:
public function display_report(){
$this->load->library('google');
$client = new Google_Client();
$client->getAccessToken();
$youtubeAnalytics = new Google_Service_YouTubeAnalytics($client);
$id = 'channel==MINE';
$start_date = '2014-01-01';
$end_date = '2016-01-20';
$optparams = array('filters' => 'video==*********');
$metrics = array('views');
$api_response = $metrics;
$api = $youtubeAnalytics->reports->query($id, $start_date, $end_date, $metric,$optparams);
$data['api_response'] = $api_response['views'];
$this->load->view('layouts/mainlay',$data);
}
I have never worked with YouTube API before, but here's a step in the right direction.
class Youtubeverify extends CI_Controller {
// Google API Keys
const CLIENT_ID = '';
const CLIENT_SECRET = '';
// We'll store the client in a class property so we can access it between methods.
protected $Client;
public function __construct () {
parent::__construct();
// setup google client.
$this->Client = new Google_Client();
$this->Client->setClientId(self::CLIENT_ID);
$this->Client->setClientSecret(self::CLIENT_SECRET);
// we call the authenication method for each request
$this->_authenticate_client();
}
// Authentication method
protected function _authenticate_client () {
//if we already have a token then we just set it in the client
//without going through authentication process again
if( $accessToken = $this->session->userdata('youtube_token') ) {
$this->Client->setAccessToken($accessToken);
}else{
// preform authentication as usual with a redirect ...etc
}
}
}

Oauth 2 $client->getAccessToken() Returns Null value

I am using codeigniter MVC framework for signing in to my site using google client library. Everything is working fine except $client->getAccessToken() when google redirects with code and I do the following code. $client->getAccessToken() return null value. Here is my code for controller function one. In this function I set my credentials to create authUrl.
public function login()
{
// Include two files from google-php-client library in controller
include_once APPPATH . 'third_party/google-api-php-client/vendor/autoload.php';
// Store values in variables from project created in Google Developer Console
$client_id = 'XXXXXX';
$client_secret = 'XXXXX';
$redirect_uri = 'path/to/mysite/login/loginGoogle';
$simple_api_key = 'XXXXXXX';
// Create Client Request to access Google API
$client = new Google_Client();
$client->setApplicationName("mysite");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setDeveloperKey($simple_api_key);
$client->addScope("https://www.googleapis.com/auth/userinfo.email");
$authUrl = $client->createAuthUrl();
$data['authUrl'] = $authUrl;
$this->load->view('login',$data);
}
And after that when google authenticate and redirects to my redirect uri which is an other controller function which is given below. and problem is in this function.
public function loginGoogle()
{
// Include two files from google-php-client library in controller
include_once APPPATH . 'third_party/google-api-php-client/vendor /autoload.php';
$client_id = 'XXXXXX';
$client_secret = 'XXXXX';
$redirect_uri = 'path/to/mysite/login/loginGoogle';
$simple_api_key = 'XXXXXXX';
// Create Client Request to access Google API
$client = new Google_Client();
$client->setApplicationName("mysite");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setDeveloperKey($simple_api_key);
$client->addScope("https://www.googleapis.com/auth/userinfo.email");
$objOAuthService = new Google_Service_Oauth2($client);
// Add Access Token to Session
if(!isset($_SESSION['access_token'])){
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$token = $client->getAccessToken();
$_SESSION['access_token'] = $token;
print_r($this -> session -> userdata());exit;
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
// Set Access Token to make Request
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
}
// Get User Data from Google and store them in $data
if ($client->getAccessToken()) {
$userData = $objOAuthService->userinfo->get();
$data['userData'] = $userData;
$_SESSION['access_token'] = $client->getAccessToken();
}}
here in second function getAccessToken return nothing and google throws expection.
It looks like you never get the refresh token. There are two different tokens, the access token expires every few hours or so, but the refresh token is only sent the one time when the redirect asks the user for permission. It needs to be stored somewhere secure and is used in the future to refresh the access token.
Here's what my codeigniter code looks like to access the Google API (this would replace your if statements in the loginGoogle function:
if($refresh_token_accessed_from_my_database) {
//If session contains no valid Access token, get a new one
if ($client->isAccessTokenExpired()) {
$client->refreshToken($refresh_token_accessed_from_my_database);
}
//We have access token now, launch the service
$this->service = new Google_Service_Calendar($client);
}
else {
//User has never been authorized, so let's ask for the ok
if (isset($_GET['code'])) {
//Creates refresh and access tokens
$credentials = $client->authenticate($_GET['code']);
//Store refresh token for further use
//I store mine in the DB, I've seen others store it in a file in a secure place on the server
$refresh_token = $credentials['refresh_token'];
//refresh_token->persist_somewhere()
//Store the access token in the session so we can get it after
//the callback redirect
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
if (!isset($_SESSION['access_token'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$this->service = new Google_Service_Calendar($client);
}
If you are running on PLESK you might want to change the permission on /var/lib/php/session to 1777.
chmod 1777 /var/lib/php/sessions

google-oauth library working in session in MVC PHP

Hi I would like to learn something from here, basically I want the access of my system to be in google account somehow I manage to do the following
get client id, redirect uris, secret keys
authentication from google account
get token
but I i felt I was doing wrong all in some part, this is the 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.
The code you have might be a bit confusing because it handles authorization url, redirect to Google, callback to your site, and save the access token at the same time.
When it comes to use the access token to perform authenticated call to the api, it's much simpler. If you have a valid access token stored in you session, you can really use it anywhere to initialize the Google Application, this way:
// client
$client = new Google_Client();
$client->setApplicationName('Google Application');
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);
// token
$client->setAccessToken($token);
Now if you want this to be available across several controllers, the most appropriate way in CodeIgniter would be to create a new library wrapping the code above.
The advantage of using a library is that they can be auto-loaded in CodeIgniter config, and are easily accessible anywhere in your code.
Example library:
<?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);
}
}
?>
Then you just have to do this to use it in your controller:
$this->load->library('someclass');
You could also create shortcuts to specific APIs. For example, if you wanted a quick access to Google Analytics API, you could do this:
<?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);
}
}
?>
And then use it this way in your controller:
$this->load->library('someclass');
$this->someclass->analytics();
Learn more about CodeIgniter libraries:
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

google analytics (401) Login Required error

i am using google analytics php api to retrieve my data. on index.php i have authorization process done which works fine and i get successful login on this page i have link to another php page where i want to display all my properties so how i can post my login credentials to properties.php and create an analytics object to retrieve all the webproperties
code index.php look like this
session_start();
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];
$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setApplicationName('XXXX');
$client->setClientId('XXXXXX');
$client->setClientSecret('XXXXX');
$client->setRedirectUri($scriptUri);
$client->setDeveloperKey('XXXXXX'); // API key
$client->setScopes('https://www.googleapis.com/auth/analytics');
$service = new Google_AnalyticsService($client);
if (isset($_GET['logout'])) { // logout: destroy token
unset($_SESSION['token']);
die('Logged out.');
}
if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) { // extract token from session and configure client
$token = $_SESSION['token'];
$client->setAccessToken($token);
}
if (!$client->getAccessToken()) { // auth call to google
$authUrl = $client->createAuthUrl();
header("Location: ".$authUrl);
die;
}
TRY{
}
?>

Categories