I wanna only ask to the user one time to access the account. I have the token but after 3600 seconds the token expires.
This is my code (working) "inside" a framework. Someone can tell how can i do it?
public function actionEvent() {
$client = new Google_Client();
$client->setApplicationName("Google Calendar Event");
$client->setAuthConfig(Yii::getAlias('@webroot') . '/calendar/client_secret.json');
$client->addScope(\Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$calendar_service = new \Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event(array(
..events here..
));
$calendarId = 'primary';
$event = $calendar_service->events->insert($calendarId, $event);
var_dump($event->htmlLink);
} else {
$redirect_uri = $this->redirect(['pop/callback']);
}
}
public function actionCallback() {
$client = new Google_Client();
$client->setApplicationName("Google Calendar Event");
$client->setAuthConfigFile(Yii::getAlias('@webroot') . '/calendar/client_secret.json');
$client->setRedirectUri('http://localhost/pop/callback');
$client->addScope(\Google_Service_Calendar::CALENDAR);
if (!isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
$this->redirect($auth_url);
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$this->redirect(['pop/event']);
}
}
Inside of session i have:
array (size=3)
'__flash' =>
array (size=0)
empty
'__id' => int 1
'access_token' =>
array (size=4)
'access_token' => string 'ya29.Ci-sf-asdfsadfsdfsd' (length=71)
'token_type' => string 'Bearer' (length=6)
'expires_in' => int 3599
'created' => int 1479326378
Thank you