1
votes

I need access any other user's google calendar freebusy using google php api. Can any one help me step by step process to implement that. I have already checked the https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query#response but nothing fruitful I got.

My Approach

session_start();

require_once ('libraries/Google/autoload.php');

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("email");
$client->addScope("profile");
$client->addScope(Google_Service_Calendar::CALENDAR);
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
$client->setAccessType('offline'); 

$service = new Google_Service_Oauth2($client);

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  exit;
}

/************************************************
  If we have an access token, we can make
  requests, else we generate an authentication URL.
 ************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $authUrl = $client->createAuthUrl();
}

$client->setApplicationName("My Calendar"); //DON'T THINK THIS MATTERS
$client->setDeveloperKey($api_key); //GET AT AT DEVELOPERS.GOOGLE.COM
$cal = new Google_Service_Calendar($client);
$calendarId = '[email protected]';

$freebusy_req = new Google_Service_Calendar_FreeBusyRequest();

$freebusy_req->setTimeMin($minTime);
$freebusy_req->setTimeMax($maxTime);
$freebusy_req->setTimeZone($time_zone);
$freebusy_req->setCalendarExpansionMax(10);
$freebusy_req->setGroupExpansionMax(10);

$item = new Google_Service_Calendar_FreeBusyRequestItem();
$item->setId($email);

$freebusy_req->setItems(array($item));

$query = $cal->freebusy->query($freebusy_req);

$response_calendar = $query->getCalendars();
$busy_obj = $response_calendar[$email]->getBusy();

But I am getting blank in terms of free busy.

1
Can you share your attempt? - Laur Ivan
@LaurIvan Thanks for your interest. I have added my code, please check it and if possible suggest me any solution. - Banty Roy
@DalmTo The code is running well, when I logged in and try to fetch freebusy, its show correct data. But my requirement is something different, I need someone else's freebusy by his calendar id (email id) and time min, max. This time empty result showing. I hope it will helps you. - Banty Roy
Might be silly, but... are you sure you have the right to see the other person's calendar? - Laur Ivan
@LaurIvan Actually they will approve my application by login once then only I can access. Could you please give me any suggestion. - Banty Roy

1 Answers

1
votes

Just make sure the calendar is public, then try google calendar freebusy method. It will definitely work.