http://livingcode.co/index.php/tag/one-time-authorization-code/
http://martinfowler.com/articles/command-line-google.html
New API doc here:
https://developers.google.com/api-client-library/php/auth/web-app
library:
https://github.com/google/google-api-php-client
index.php
<?php
// require_once 'google-api-php-client/autoload.php';
require_once 'google-api-php-client/src/Google/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setAccessType("offline");
$client->addScope(Google_Service_Calendar::CALENDAR);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
// echo $client->getAccessToken();
$cal = new Google_Service_Calendar($client);
$params = array(
//CAN'T USE TIME MIN WITHOUT SINGLEEVENTS TURNED ON,
//IT SAYS TO TREAT RECURRING EVENTS AS SINGLE EVENTS
'singleEvents' => true,
'orderBy' => 'startTime',
'timeMin' => date(DateTime::ATOM),//ONLY PULL EVENTS STARTING TODAY
'maxResults' => 7 //ONLY USE THIS IF YOU WANT TO LIMIT THE NUMBER
//OF EVENTS DISPLAYED
);
$events = $cal->events->listEvents('primary', $params);
$calendar = $cal->calendars->get('primary');
$event = new Google_Service_Calendar_Event();
$event->setSummary('Happy Birth day');
$event->setLocation('Palau');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2015-09-27T10:00:00.000-07:00');
// $start->setDate('2015-10-26');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2015-10-20T10:25:00.000-07:00');
// $end->setDate('2015-10-27');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('mtischer53@gmail.com');
// ...
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;
// $createdEvent = $cal->events->insert('primary', $event);
// echo $createdEvent->getId();
$createdEvent = $cal->events->quickAdd(
'primary',
'What have I done on Sep 27 10am-10:25am');
echo "<br/> >>> ". $createdEvent->getId();
echo "<br/> End ";
// print_r($events);
// echo $calendar->getSummary();
// die('hard');
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
callback.php
<?php
require_once 'google-api-php-client/src/Google/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setAccessType("offline");
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
//$client->setRedirectUri('http://google-cal.org');
$client->addScope(Google_Service_Calendar::CALENDAR);
if (!isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
client_secrets.json:
{"web":{"client_id":"43391496547-09fd8g7ftibu7ea5pkdk4tegtm8m3eoq.apps.googleusercontent.com","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"hahaha9t2zskXCBfGFYHg","redirect_uris":["http://gyoumukanri-test.org/site/auth?authclient=google"],"javascript_origins":["http://www.gyoumukanri-test.org"]}}
Web console app config:
http://martinfowler.com/articles/command-line-google.html
New API doc here:
https://developers.google.com/api-client-library/php/auth/web-app
library:
https://github.com/google/google-api-php-client
index.php
<?php
// require_once 'google-api-php-client/autoload.php';
require_once 'google-api-php-client/src/Google/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setAccessType("offline");
$client->addScope(Google_Service_Calendar::CALENDAR);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
// echo $client->getAccessToken();
$cal = new Google_Service_Calendar($client);
$params = array(
//CAN'T USE TIME MIN WITHOUT SINGLEEVENTS TURNED ON,
//IT SAYS TO TREAT RECURRING EVENTS AS SINGLE EVENTS
'singleEvents' => true,
'orderBy' => 'startTime',
'timeMin' => date(DateTime::ATOM),//ONLY PULL EVENTS STARTING TODAY
'maxResults' => 7 //ONLY USE THIS IF YOU WANT TO LIMIT THE NUMBER
//OF EVENTS DISPLAYED
);
$events = $cal->events->listEvents('primary', $params);
$calendar = $cal->calendars->get('primary');
$event = new Google_Service_Calendar_Event();
$event->setSummary('Happy Birth day');
$event->setLocation('Palau');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2015-09-27T10:00:00.000-07:00');
// $start->setDate('2015-10-26');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2015-10-20T10:25:00.000-07:00');
// $end->setDate('2015-10-27');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('mtischer53@gmail.com');
// ...
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;
// $createdEvent = $cal->events->insert('primary', $event);
// echo $createdEvent->getId();
$createdEvent = $cal->events->quickAdd(
'primary',
'What have I done on Sep 27 10am-10:25am');
echo "<br/> >>> ". $createdEvent->getId();
echo "<br/> End ";
// print_r($events);
// echo $calendar->getSummary();
// die('hard');
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
callback.php
<?php
require_once 'google-api-php-client/src/Google/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setAccessType("offline");
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
//$client->setRedirectUri('http://google-cal.org');
$client->addScope(Google_Service_Calendar::CALENDAR);
if (!isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
client_secrets.json:
{"web":{"client_id":"43391496547-09fd8g7ftibu7ea5pkdk4tegtm8m3eoq.apps.googleusercontent.com","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"hahaha9t2zskXCBfGFYHg","redirect_uris":["http://gyoumukanri-test.org/site/auth?authclient=google"],"javascript_origins":["http://www.gyoumukanri-test.org"]}}
Web console app config:
Comments
Post a Comment