-
pippuccio76
Senior Member
-
Posts: 465
Threads: 199
Joined: Jun 2017
Reputation:
2
Hi sorry for english , i try to edit some classes found on internet :
this is my Googleplus.php library:
Code: <?php
namespace App\Libraries;
// https://console.cloud.google.com/apis
/*
https://github.com/aididalam/CodeIgniter-Google-Calendar
*/
use Google\Client as Google_client;
use Google\Service\Calendar as Google_Service_Calendar ;
use Google\Service\Calendar\Event as Google_Service_Calendar_Event ;
use Google\Service\Oauth2 as Google_Service_Oauth2;
class Googleplus
{
private $google_client;
public function __construct()
{
$this->google_client = $this->googleGetClient();
}
public function googleGetClient(){
$google_client = new Google_client();
$google_client->setAuthConfig(WRITEPATH.'google_calendar/credentials.json');
$google_client->setRedirectUri(base_url().'/admin_google_calendar/login');
$google_client->addScope('email');
$google_client->addScope('profile');
$google_client->addScope(Google_Service_Calendar::CALENDAR);
if(isset($_GET["code"]))
{
$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);
if(!isset($token["error"]))
{
$google_client->setAccessToken($token['access_token']);
session()->set('access_token', $token['access_token']);
$google_service = new Google_Service_Oauth2($google_client);
$data_session = $google_service->userinfo->get();
$current_datetime = date('Y-m-d H:i:s');
//insert data
session()->set('user_data' , array(
'login_oauth_uid' => $data_session['id'],
'first_name' => $data_session['given_name'],
'last_name' => $data_session['family_name'],
'email_address' => $data_session['email'],
'profile_picture' => $data_session['picture'],
'created_at' => $data_session
));
}
}
return $google_client;
}//END get_Client
// check login session
public function googleIsLogin() {
$token = session()->get('access_token');
if ($token) {
$this->google_client->setAccessToken($token);
}
return $token;
}//end isLogin
public function google_logout()
{
session()->remove('access_token');
}//end google_logout
public function loginUrl()
{
return $this->google_client->createAuthUrl();
}
public function getAccessToken()
{
return $this->google_client->getAccessToken();
}
public function refreshToken()
{
$this->google_client->getRefreshToken();
$token = $this->getAccessToken();
session()->set('access_token' ,$token['access_token']);
}
public function revokeToken()
{
return $this->google_client->revokeToken();
}
public function getUser()
{
$google_ouath = new Google_Service_Oauth2($this->google_client);
return (object)$google_ouath->userinfo->get();
}
public function isAccessTokenExpired()
{
return $this->google_client->isAccessTokenExpired();
}
public function getClient()
{
return $this->google_client;
}
}
and this is my controller :
Code: <?php
namespace App\Controllers;
use Google\Service\Calendar as Google_Service_Calendar ;
use Google\Service\Calendar\Event as Google_Service_Calendar_Event ;
use App\Libraries\Googleplus;
class Admin_google_calendar extends BaseController {
public function login()
{
$google_library =new Googleplus();
$google_client = $google_library->GetClient();
$login_button = '';
//print_r($_SESSION);
//die();
if(!session()->get('access_token'))
{
$login_button = '<a class="btn btn-danger" href="'.$google_client->createAuthUrl().'">Google Login</a>';
$data['login_button'] = $login_button;
echo view('empty_view',$data);
echo view('admin/google_calendar/google_login');
}
else
{
return redirect()->to('/admin_google_calendar/show_calendar');
}
}
public function getCalendar() {
$google_library =new Googleplus();
if (!$google_library->googleIsLogin()) {
return redirect()->to('admin_google_calendar/login');
} else {
$google_client = $google_library->getClient();
//$client = googleGetClient();
$service = new Google_Service_Calendar($google_client);
// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
//funziona
/*
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => true,
'timeMin' => date('c'),
);
*/
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => true,
'timeMin' => date('c'),
'timeMin' => '2022-08-01T00:00:00-02:00',
'timeMax' => '2022-08-31T00:00:00-02:00',
);
$results = $service->events->listEvents($calendarId, $optParams);
$events = $results->getItems();
if (empty($events)) {
print "No upcoming events found.\n";
} else {
print "Upcoming events:\n";
foreach ($events as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
printf("%s (%s)\n", $event->getSummary(), $start);
}
}
}
}
// oauth method
public function oauth() {
$code = $this->input->get('code', true);
$this->oauthLogin($code);
return redirect()->to(base_url(), 'refresh');
}
// oauthLogin
public function oauthLogin($code) {
$google_library =new Googleplus();
$google_client = $google_library->googleGetClient();
$login = $google_client->authenticate($code);
if ($login) {
$token = $google_client->getAccessToken();
session()->set('google_calendar_access_token', $token);
session()->set('is_authenticate_user', TRUE);
return true;
}
}
// add google calendar event
public function addEvent() {
$google_library =new Googleplus();
if (!$google_library->googleIsLogin()) {
session_destroy();
return redirect()->to(base_url(), 'refresh');
} else {
$json = array();
$calendarId = 'primary';
//$calendarId = '[email protected]';
//$post = $this->request->getPost();
$post['summary'] ='Test Insert';
$post['startDate'] = '2022-07-31';
$post['startTime'] ='07:00';
$post['endDate'] = '2022-07-31';
$post['endTime'] = '08:00';
$post['description'] = 'Prova inserimento da codeigniter';
if(empty(trim($post['summary']))){
$json['error']['summary'] = 'Please enter summary';
}
// start date time validation
if(empty(trim($post['startDate'])) && empty($post['startTime'])){
$json['error']['startdate'] = 'Please enter start date time';
}
if(empty(trim($post['endDate'])) && empty($post['endTime'])){
$json['error']['enddate'] = 'Please enter end date time';
}
if(empty(trim($post['description']))){
$json['error']['description'] = 'Please enter description';
}
if(empty($json['error'])){
$event = array(
'summary' => $post['summary'],
'start' => $post['startDate'].'T'.$post['startTime'].':00+02:00',
'end' => $post['endDate'].'T'.$post['endTime'].':00+02:00',
'description' => $post['description'],
);
$data = $this->actionEvent($calendarId, $event);
if ($data->status == 'confirmed') {
$json['message'] = 1;
} else {
$json['message'] = 0;
}
}
//$this->output->set_header('Content-Type: application/json');
echo json_encode($json);
}
}
// actionEvent
public function actionEvent($calendarId, $data) {
$google_library =new Googleplus();
$google_client = $google_library->googleGetClient();
$google_client->setAccessToken(session()->get('access_token'));
$google_service_calendar= new Google_Service_Calendar($google_client);
//Date Format: 2016-06-18T17:00:00+03:00
$event = new Google_Service_Calendar_Event(
array(
'summary' => $data['summary'],
'description' => $data['description'],
'start' => array(
'dateTime' => $data['start'],
'timeZone' => 'Europe/Rome',
),
'end' => array(
'dateTime' => $data['start'],
'timeZone' => 'Europe/Rome',
),
'attendees' => array(
array('email' => '[email protected]'),
),
)
);
return $google_service_calendar->events->insert($calendarId, $event);
}
public function show_calendar()
{
$data=[];
$google_library =new Googleplus();
if (!$google_library->googleIsLogin()) {
//session_destroy();
return redirect()->to('admin_google_calendar/login');
} else {
//DEBUG
//echo 'Inside';
//die();
$google_client = $google_library->googleGetClient();
$google_client->setAccessToken(session()->get('access_token'));
$service = new Google_Service_Calendar($google_client);
// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
'orderBy' => 'startTime',
'singleEvents' => true,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
$eventi = $results->getItems();
$lista_eventi= [];
$n=0;
foreach ($eventi as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
$end = $event->end->dateTime;
if (empty($start)) {
$end = $event->end->date;
}
$lista_eventi[$n]['id'] = $event->id;
$lista_eventi[$n]['title'] = $event->getSummary();
$lista_eventi[$n]['start'] = $start;
$lista_eventi[$n]['end'] = $end;
$lista_eventi[$n]['backgroundColor'] = "#00a65a";
$n++;
}
/* print_r($lista_eventi);
die();*/
}
$data['events'] = $lista_eventi;
echo view('empty_view',$data );
echo view('admin/google_calendar/show_calendar',$data);
}
public function carica_eventi_calendario_ajax()
{
$google_library =new Googleplus();
if (($this->request->getMethod() === 'post')){
$post = $this->request->getPost();
$google_client = $google_library->googleGetClient();
$google_client->setAccessToken(session()->get('access_token'));
//$client = googleGetClient();
$service = new Google_Service_Calendar($google_client);
// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
'orderBy' => 'startTime',
'singleEvents' => true,
'timeMin' => date('c'),
'timeMin' => $post['start'].'T00:00:00-02:00',
'timeMax' => $post['end'].'T00:00:00-02:00',
);
$results = $service->events->listEvents($calendarId, $optParams);
$eventi = $results->getItems();
$lista_eventi= [];
$n=0;
foreach ($eventi as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
$end = $event->end->dateTime;
if (empty($start)) {
$end = $event->end->date;
}
$lista_eventi[$n]['id'] = $event->id;
$lista_eventi[$n]['title'] = $event->getSummary();
$lista_eventi[$n]['start'] = $start;
$lista_eventi[$n]['end'] = $end;
$lista_eventi[$n]['backgroundColor'] = "#00a65a";
$n++;
}
echo json_encode($lista_eventi);
}else{
echo 'alert("dati non ricevuti")';
}
}//end carica_eventi_calendario_ajax
public function test_access_token($val=NULL)
{
$google_library =new Googleplus();
$google_client = $google_library->getClient();
if($google_library->isAccessTokenExpired() ){
$token = session()->get('access_token');
$google_client->setAccessToken($token);
if ($google_client->isAccessTokenExpired()) {
$google_client->fetchAccessTokenWithRefreshToken($google_client->getRefreshToken());
$google_client->setAccessToken($google_client->getAccessToken());
$_SESSION['access_token'] =$token= $google_client->getAccessToken();
echo 'refereshato';
die();
}
echo 'Scaduto';
//print_r(redirect()->back());
}else{
echo 'Attivo';
}
}
public function logout_google($val=NULL)
{
$google_library =new Googleplus();
$google_library->google_logout();
return redirect()->to('admin_google_calendar/login');
}
} It work but if the access token expire i have an exception :
i try to use this code in lbrary :
Code: // check login session
public function googleIsLogin() {
$token = session()->get('access_token');
if ($token) {
$this->google_client->setAccessToken($token);
}
if ($this->google_client->isAccessTokenExpired()) {
// Set the access token on the client.
$this->google_client->setAccessToken($_SESSION['access_token']);
// Refresh the access token if it's expired.
if ($this->google_client->isAccessTokenExpired()) {
$this->google_client->fetchAccessTokenWithRefreshToken($this->google_client->getRefreshToken());
$this->google_client->setAccessToken($this->google_client->getAccessToken());
session()->set('access_token', $this->google_client->getAccessToken());
}
}
return $token;
}//end isLogin
as in this class https://github.com/LindaLawton/Google-AP...cation.php
but have this error :
refresh token must be passed in or set as part of setAccessToken
How can fix it ?
|