53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
<?php
|
|
//Lib from: https://github.com/googleapis/google-api-php-client/releases
|
|
require_once __DIR__ . '/google-api-php-client/vendor/autoload.php';
|
|
|
|
session_start();
|
|
|
|
// Google Client einrichten
|
|
$client = new Google_Client();
|
|
$client->setAuthConfig('credentials.json'); //Download from Google Cloud Admin Pannel
|
|
$client->setRedirectUri('<URL>/oauth2callback.php');
|
|
$client->addScope(Google_Service_Calendar::CALENDAR);
|
|
$client->setAccessType('offline');
|
|
|
|
// Wenn kein Token da ist → Weiterleitung zum Login
|
|
if (!isset($_SESSION['access_token'])) {
|
|
$authUrl = $client->createAuthUrl();
|
|
header('Location: ' . $authUrl);
|
|
exit;
|
|
}
|
|
|
|
// Token setzen
|
|
$client->setAccessToken($_SESSION['access_token']);
|
|
|
|
// Falls Token abgelaufen ist, erneuern
|
|
if ($client->isAccessTokenExpired()) {
|
|
unset($_SESSION['access_token']);
|
|
header('Location: /');
|
|
exit;
|
|
}
|
|
|
|
// Kalender-Service
|
|
$service = new Google_Service_Calendar($client);
|
|
|
|
// Termin erstellen
|
|
$event = new Google_Service_Calendar_Event([
|
|
'summary' => 'Privater Termin',
|
|
'location' => 'Zuhause',
|
|
'description' => 'PHP Google Calendar API ohne Composer',
|
|
'start' => [
|
|
'dateTime' => '2025-06-24T15:00:00+02:00',
|
|
'timeZone' => 'Europe/Berlin',
|
|
],
|
|
'end' => [
|
|
'dateTime' => '2025-06-24T16:00:00+02:00',
|
|
'timeZone' => 'Europe/Berlin',
|
|
],
|
|
]);
|
|
|
|
$calendarId = 'primary';
|
|
$event = $service->events->insert($calendarId, $event);
|
|
|
|
echo "Termin erstellt: <a href='" . $event->htmlLink . "'>Anzeigen</a>";
|