[RESOLU] php + Google Calendar afin de générer X évenements

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : [RESOLU] php + Google Calendar afin de générer X évenements

Re: php + Google Calendar afin de générer X évenements

par LeVoL » 30 août 2015, 20:16

Merci @arthur, et je ferme l'incident :) ca m'a permis de débloqué le schmillllblik

Re: php + Google Calendar afin de générer X évenements

par @rthur » 20 juil. 2015, 08:11

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

php + Google Calendar afin de générer X évenements

par LeVoL » 20 juil. 2015, 01:46

Bonsoir,
Je m'en réfère à vous les copains parceque je sèche depuis 3 jours la dessus... (mon anglais et mes compétences doivent être trop short)

Je tente en vain de pouvoir générer autant d'événement dans un calendrier google en fonction d'un nombre X de tuples dans ma bdd...

tous les tutos concernant google calendar pour l'ajout d'event via ses pages php précisent bien de créer un compte https://console.developers.google.com/

Sauf erreur de ma part je génère donc des identifiants pour un compte de "service" -> il ne faut pas avoir besoin de s'authentifier sur ma page php pour generer les events...
J'ai donc les infos suivantes :

Compte de service
Identifiant client
xxx.apps.googleusercontent.com
Adresse e-mail
[email protected]
Empreintes de certificat
xxx

maintenant je ne sais pas comment ajouter les librairies nécessaires... ni même compris comment utiliser les clef P12, Json et compagnie...

Si une âme charitable pourrait bien m'accompagner dans la réalisation de mon schmilblik ?

Merci !