Bonjour,
Les API de Google sont pas forcément faciles à prendre en main au début.
Voici la librairie officielle PHP de Google :
https://github.com/google/google-api-php-client
Et voici ci-dessous un exemple qui me semble complet (je n'ai pas testé) pour faire ce que tu veux :
<?php
/*
Say you have some kind of PHP based web application that needs to set appoint-
ments in (various) Google calendars. How do you do this?
There are some steps to take. Here they are:
a) Log in into the Google account you want to use to manage your projects.
b) Go to https://code.google.com/apis/console/
c) Create a project. You will get issued a project number, in our example
we'll use 99999999999.
d) Allow the project to use the calendar API:
-> services (left pane), then find 'calendar api' and set the button 'on'
e) Create a Service Account.
-> API access (left pane); select 'create service account'
Part of the procedure is that you'll get a PKCS#12 bundle, which you can
download and should store on your appserver in a secure location. Your
app should be able to access it, but outsiders should not be allowed to
download it.
f) download and install the Google PHP libraries.
Google says "The mechanics of the interaction between Google and your
application require applications to create and cryptographically sign JSON Web
Tokens (JWTs). Developers are strongly encouraged to use a library to perform
these tasks. Writing this code without the use of a library that abstracts
token creation and signing is prone to errors that can have a severe impact
on the security of your application."
The PHP libs can be found here:
https://code.google.com/p/google-api-php-client/wiki/OAuth2
g) to enable your application to get access to other folks calendars (or to
your own, for that matter), the owner of that calendar (somebody that has
the role 'owner' there) has to allow your system account's 'client id' to
access his calendar. If you want to be able to have your system make an
appointment, it needs at least write rights.
See: http://support.google.com/a/bin/answer.py?hl=en&answer=162106
** Read more about service accounts here:
https://developers.google.com/accounts/docs/OAuth2#serviceaccount
h) finally, just go through this example and replace the various
account related data (marked with LOUD COMMENTS) with your own. Then run
the script and admire the resulting appointment in your Google calendar.
Heinrich Wilhelm Kloepping Sr, CISSP
Sat Nov 10 17:20:40 GMT+1 2012
*/
/* THIS IS THE PATH TO THE GOOGLE LIBRARIES
*/
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
/* CHANGE INTO YOUR SERVICE_ACCOUNT_NAME.
* This is also the identity others need to allow as owner or writer
* in their calendars if they want your application to access your calendar
*/
define('SERVICE_ACCOUNT_NAME', '[email protected]');
/* THE PATH TO THE SECRET KEY GENERATED WHEN YOU REQUESTED THE
* SERVICE ACCOUNT. The key's name contains it's public key
* fingerprint, represented below by the string <longhexstring>
*/
define('KEY_FILE', '/some/path/to/the/<longhexstring>-privatekey.p12');
$client = new Google_Client();
$client->setApplicationName("My TEST Application");
/* Note: make sure to call $client->setUseObjects(true) if you want to see
* objects returned instead of data (this example code uses objects)
*/
$client->setUseObjects(true);
/* If you already have a session token, use it. Normally this token is
* stored in a database and you'd need to retrieve it from there. For
* this demo we'll simply start a new session each time.
*/
session_start();
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
/* Load the key in PKCS 12 format - remember: this is the file you had to
* download when you created the Service account on the API console.
*/
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key)
);
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
/* ------------------------- We are now properly authenticated ------------------- */
$cal = new Google_CalendarService($client);
$event = new Google_Event();
$event->setSummary('Dinner at Henk'); /* what to do, summary of the appointment */
$event->setLocation('Slochteren'); /* yes, it exists */
/* Now, set the start date/time
*/
$start = new Google_EventDateTime();
$start->setDateTime('2012-11-10T19:00:00.000+01:00'); /* Or e.g. 2010-08-26T10:40:00+02:00 */
$event->setStart($start);
/* Now, set the end date/time
*/
$end = new Google_EventDateTime();
$end->setDateTime('2012-11-10T22:00:00.000+01:00'); /* 2010-08-26T10:40:00+02:00 */
$event->setEnd($end);
/* For now I just set one attendee, but you can create lists of many if you want to
*/
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail('[email protected]');
$attendees = array($attendee1);
$event->attendees = $attendees;
/* CREATE THE EVENT IN THE PROPER CALENDAR
*/
$createdEvent = $cal->events->insert('[email protected]', $event);
print "<html><body>";
echo "<pre>I created a calendar entry, it's id is '" . $createdEvent->id . "'</pre>";
print "</body></html>";
/* Here should be some code to store the token in the database again.
* Just to reminds us we put some useless code here ;-P
*/
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
?>
Source
Bonjour,
Les API de Google sont pas forcément faciles à prendre en main au début.
Voici la librairie officielle PHP de Google : https://github.com/google/google-api-php-client
Et voici ci-dessous un exemple qui me semble complet (je n'ai pas testé) pour faire ce que tu veux :
[php]<?php
/*
Say you have some kind of PHP based web application that needs to set appoint-
ments in (various) Google calendars. How do you do this?
There are some steps to take. Here they are:
a) Log in into the Google account you want to use to manage your projects.
b) Go to https://code.google.com/apis/console/
c) Create a project. You will get issued a project number, in our example
we'll use 99999999999.
d) Allow the project to use the calendar API:
-> services (left pane), then find 'calendar api' and set the button 'on'
e) Create a Service Account.
-> API access (left pane); select 'create service account'
Part of the procedure is that you'll get a PKCS#12 bundle, which you can
download and should store on your appserver in a secure location. Your
app should be able to access it, but outsiders should not be allowed to
download it.
f) download and install the Google PHP libraries.
Google says "The mechanics of the interaction between Google and your
application require applications to create and cryptographically sign JSON Web
Tokens (JWTs). Developers are strongly encouraged to use a library to perform
these tasks. Writing this code without the use of a library that abstracts
token creation and signing is prone to errors that can have a severe impact
on the security of your application."
The PHP libs can be found here:
https://code.google.com/p/google-api-php-client/wiki/OAuth2
g) to enable your application to get access to other folks calendars (or to
your own, for that matter), the owner of that calendar (somebody that has
the role 'owner' there) has to allow your system account's 'client id' to
access his calendar. If you want to be able to have your system make an
appointment, it needs at least write rights.
See: http://support.google.com/a/bin/answer.py?hl=en&answer=162106
** Read more about service accounts here:
https://developers.google.com/accounts/docs/OAuth2#serviceaccount
h) finally, just go through this example and replace the various
account related data (marked with LOUD COMMENTS) with your own. Then run
the script and admire the resulting appointment in your Google calendar.
Heinrich Wilhelm Kloepping Sr, CISSP
Sat Nov 10 17:20:40 GMT+1 2012
*/
/* THIS IS THE PATH TO THE GOOGLE LIBRARIES
*/
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
/* CHANGE INTO YOUR SERVICE_ACCOUNT_NAME.
* This is also the identity others need to allow as owner or writer
* in their calendars if they want your application to access your calendar
*/
define('SERVICE_ACCOUNT_NAME', '
[email protected]');
/* THE PATH TO THE SECRET KEY GENERATED WHEN YOU REQUESTED THE
* SERVICE ACCOUNT. The key's name contains it's public key
* fingerprint, represented below by the string <longhexstring>
*/
define('KEY_FILE', '/some/path/to/the/<longhexstring>-privatekey.p12');
$client = new Google_Client();
$client->setApplicationName("My TEST Application");
/* Note: make sure to call $client->setUseObjects(true) if you want to see
* objects returned instead of data (this example code uses objects)
*/
$client->setUseObjects(true);
/* If you already have a session token, use it. Normally this token is
* stored in a database and you'd need to retrieve it from there. For
* this demo we'll simply start a new session each time.
*/
session_start();
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
/* Load the key in PKCS 12 format - remember: this is the file you had to
* download when you created the Service account on the API console.
*/
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key)
);
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
/* ------------------------- We are now properly authenticated ------------------- */
$cal = new Google_CalendarService($client);
$event = new Google_Event();
$event->setSummary('Dinner at Henk'); /* what to do, summary of the appointment */
$event->setLocation('Slochteren'); /* yes, it exists */
/* Now, set the start date/time
*/
$start = new Google_EventDateTime();
$start->setDateTime('2012-11-10T19:00:00.000+01:00'); /* Or e.g. 2010-08-26T10:40:00+02:00 */
$event->setStart($start);
/* Now, set the end date/time
*/
$end = new Google_EventDateTime();
$end->setDateTime('2012-11-10T22:00:00.000+01:00'); /* 2010-08-26T10:40:00+02:00 */
$event->setEnd($end);
/* For now I just set one attendee, but you can create lists of many if you want to
*/
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail('
[email protected]');
$attendees = array($attendee1);
$event->attendees = $attendees;
/* CREATE THE EVENT IN THE PROPER CALENDAR
*/
$createdEvent = $cal->events->insert('
[email protected]', $event);
print "<html><body>";
echo "<pre>I created a calendar entry, it's id is '" . $createdEvent->id . "'</pre>";
print "</body></html>";
/* Here should be some code to store the token in the database again.
* Just to reminds us we put some useless code here ;-P
*/
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
?>[/php][url=https://groups.google.com/forum/#!topic/google-calendar-api/MySzyAXq12Q]Source[/url]