I need transparent access to google calendar via php

0

I have the code that connects me to my google calendar account, but every time I ask for a username and password. I would need the authentication to be done automatically (that is, to save me the step of the second if). Here I pass a copy of my code:

<html>
 <head>
	<link rel="stylesheet" href="actuacions.css" type="text/css" />
 </head>
 <body>
	<?php
//	require_once '../wp-content/plugins/google-calendar-events/vendor/autoload.php';
	require_once './google-api-php-client/vendor/autoload.php';
	
	// funció que em retorna el nom del mes en funció del número del mes passat en string
	function nomMes ($mes)
	{
		switch ($mes){
			case '01': return "Gener";
			case '02': return "Febrer";
			case '03': return "Març";
			case '04': return "Abril";
			case '05': return "Maig";
			case '06': return "Juny";
			case '07': return "Juliol";
			case '08': return "Agost";
			case '09': return "Setembre";
			case '10': return "Octubre";
			case '11': return "Novembre";
			case '12': return "Desembre";
		}
	}
	// funció que em retorna si el nom que se li passa correspon a una diada o no	
	function esDiada($nom)
	{
		// Criteri 1: Criba per majúscules, minúscules -> Si tot són MAJÚSCULES és una Diada
		return ctype_upper(str_replace(' ', '', $nom));
	}

		session_start();
		// print_r($_SESSION);
		if(isset($_GET["logout"])){
		  session_destroy();
		}

		$client = new Google_Client();
		$redirect_uri ='url.php';
		$client->setClientId('miclienteId');
		$client->setClientSecret('miclienteSecreto');
		$client->setRedirectUri($redirect_uri);
		$client->addScope('profile');
		$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
		$client->setAccessType('offline');		

		print_r($client->getAccessToken());
		$authUrl = $client->createAuthUrl();
		if (isset($_GET['code'])) {
			echo '<br /> entro primer if';
		 $client->authenticate($_GET['code']);
		 $_SESSION['access_token'] = $client->getAccessToken();
		 header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
		}
		if (!$client->getAccessToken() && !isset($_SESSION['access_token'])) {
			echo '<br /> entro segon if';
			print "<a class='login' href='$authUrl'>Conectar</a>";
		}        
		if (isset($_SESSION['access_token'])) {
//			echo '<br /> entro tercer if';
//			print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>Salir</a><br>";
			$client->setAccessToken($_SESSION['access_token']);
			$service = new Google_Service_Calendar($client);
			$optParams = array(
//				'maxResults' => 10,
				'orderBy' => 'startTime',
				'singleEvents' => TRUE,
				'timeMin' => date('c'),
			);
//			print_r($optParams);
			$results = $service->events->listEvents('primary', $optParams);
//			print_r($results);
			if (count($results->getItems()) == 0) {
			  print "<h3>No hay Eventos</h3>";
			} else {
			  //print "<h3>Proximos Eventos</h3>";
			  $avui = date('Y-m-d');
			  //print "<h3>".$avui."</h3>";
			  //echo "<table border=1>";
			  
			  foreach ($results->getItems() as $event) {
				
				  $start = $event->start->dateTime;

				  $start = substr($start,0,25);

					if (empty($start)) {
					  $start = $event->start->date;
					}
					$nomDiada = $event->getSummary();
					if (esDiada($nomDiada)){
//						print_r($start.'</br>');
						$start_dia = substr($start,8,2);
//						print_r($start_dia.'</br>');
						$start_mes = nomMes(substr($start,5,2));
						$start_hora = substr($start,11,5);
						if (empty($start_hora))
							$start_hora = " hora per determinar";
						else
							$start_hora = " a les ".$start_hora;
						$descripcio = $event->description;
						$lloc = $event->location;
						if (!empty($lloc))
							$lloc = " a ".$lloc;
						echo'<div class="actuacio">';
						echo' <div class="data">';
						echo' 	<div class="mes">'.$start_mes.'</div>';
						echo' 	 <div class="dia">'.$start_dia.'</div>';
						echo' </div>';
						echo' <div class="detalls">';
						echo'  <div class="titol">'.$nomDiada.'</div>';
						echo'  <div class="descripcio">'.$descripcio.'</div>';
						echo'  <div class="ubicacio">'.$start_dia.' de '.$start_mes.$start_hora.$lloc.'</div>';
						echo' </div>';
						echo'</div>';
					}
			  }
			}
		}
	?>
 </body>
</html>
    
asked by Josep Ropals 19.08.2017 в 19:23
source

0 answers