0

I use to make appointments a calendar based on FULLCALENDAR with backup of the rdv in a database mysql on my own local server. So far, so good. I wish I could synchronize the rdvs thus taken, on the google calendar and inversely be able to import into my database mysql the rdvs taken on the account of google on line. I tried to understand and read the API doc of google calendar here and here I only get error messages. I tried to do it directly in fullcalendar but the calendar must be declared in public, something I can not do. The ideal would be to be able to make server calls in php (login and password of google clandier backup in local mysql database), for which I create my API key and a service account, but again I do not get only error messages. Would there be a link or a really functional tutorial that I could use to learn I tried with this link but never succeeded thanks in advance.

EDIT : I try this.

quickstart.php

<?php
require_once __DIR__ . '/vendor/autoload.php';


define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/calendar-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_id.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-php-quickstart.json
define('SCOPES', implode(' ', array(
  Google_Service_Calendar::CALENDAR_READONLY)
));

if (php_sapi_name() != 'cli') {
  throw new Exception('This application must be run on the command line.');
}

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient() {
  $client = new Google_Client();
  $client->setApplicationName(APPLICATION_NAME);
  $client->setScopes(SCOPES);
  $client->setAuthConfig(CLIENT_SECRET_PATH);
  $client->setAccessType('offline');

  // Load previously authorized credentials from a file.
  $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
  if (file_exists($credentialsPath)) {
    $accessToken = json_decode(file_get_contents($credentialsPath), true);
  } else {
    // Request authorization from the user.
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:\n%s\n", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for an access token.
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

    // Store the credentials to disk.
    if(!file_exists(dirname($credentialsPath))) {
      mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, json_encode($accessToken));
    printf("Credentials saved to %s\n", $credentialsPath);
  }
  $client->setAccessToken($accessToken);

  // Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
  }
  return $client;
}

/**
 * Expands the home directory alias '~' to the full path.
 * @param string $path the path to expand.
 * @return string the expanded path.
 */
function expandHomeDirectory($path) {
  $homeDirectory = getenv('HOME');
  if (empty($homeDirectory)) {
    $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
  }
  return str_replace('~', realpath($homeDirectory), $path);
}

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar($client);

// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
  'maxResults' => 10,
  'orderBy' => 'startTime',
  'singleEvents' => TRUE,
  'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);

if (count($results->getItems()) == 0) {
  print "No upcoming events found.\n";
} else {
  print "Upcoming events:\n";
  foreach ($results->getItems() as $event) {
    $start = $event->start->dateTime;
    if (empty($start)) {
      $start = $event->start->date;
    }
    printf("%s (%s)\n", $event->getSummary(), $start);
  }
}

php quickstart.php

PHP Fatal error:  Uncaught InvalidArgumentException: missing the required redirect URI in /Users/krislec/Desktop/vendor/google/auth/src/OAuth2.php:648
Stack trace:
#0 /Users/krislec/Desktop/vendor/google/apiclient/src/Google/Client.php(340): Google\Auth\OAuth2->buildFullAuthorizationUri(Array)
#1 /Users/krislec/Desktop/quickstart.php(36): Google_Client->createAuthUrl()
#2 /Users/krislec/Desktop/quickstart.php(75): getClient()
#3 {main}
  thrown in /Users/krislec/Desktop/vendor/google/auth/src/OAuth2.php on line 648

Fatal error: Uncaught InvalidArgumentException: missing the required redirect URI in /Users/krislec/Desktop/vendor/google/auth/src/OAuth2.php:648
Stack trace:
#0 /Users/krislec/Desktop/vendor/google/apiclient/src/Google/Client.php(340): Google\Auth\OAuth2->buildFullAuthorizationUri(Array)
#1 /Users/krislec/Desktop/quickstart.php(36): Google_Client->createAuthUrl()
#2 /Users/krislec/Desktop/quickstart.php(75): getClient()
#3 {main}
  thrown in /Users/krislec/Desktop/vendor/google/auth/src/OAuth2.php on line 648
kris Lec
  • 41
  • 9
  • "again I do not get only error messages". Well if definitely can be done via PHP and a service account, so presumably you are doing something not quite right. But we can't fix what we can't see. Share enough code and info about the error messages so we can understand the problem, and maybe we can help (but obviously you should blank out your API Key / Clients IDs etc etc). Without any concrete code and detail, there's nothing we can do - "I get errors" doesn't tell us anything useful about your situation. See https://stackoverflow.com/help/mcve for guidance on how to describe your problem – ADyson Oct 30 '17 at 09:51
  • Yes, off course. But I made so much try not only with one code... I was hopping for a new tutorial to give an another try. I'm comming soon. – kris Lec Oct 31 '17 at 10:26
  • Didn't you find Google's own example at https://developers.google.com/google-apps/calendar/quickstart/php? – ADyson Oct 31 '17 at 10:30
  • Yes, saw. But really hard for me to anderstand. First, I didn't installed correctly the binary with composer. Now it's ok... but still doesn't work: see next code. Thanks. – kris Lec Nov 03 '17 at 12:33
  • "missing the required redirect URI" seems fairly clear. You forgot to provide a parameter that's necessary. You need to tell google where to redirect back to after the user has authenticated. – ADyson Nov 03 '17 at 13:54
  • Why redirect URI ! It not simple... – kris Lec Nov 03 '17 at 21:53
  • So I went to my "OAuth 2.0 client IDs" google page to add a restriction, I type in Authorized redirect URIs "http://localhost" and save it. But I'm trying by cli the quickstart/php. Same errors. Sorry for my poor english and my poor ability to code ! – kris Lec Nov 03 '17 at 22:00

0 Answers0