如何使用谷歌php api访问任何其他谷歌日历

时间:2016-03-17 09:28:00

标签: calendar google-calendar-api google-api-php-client

我需要使用google php api访问任何其他用户的Google日历freebusy。任何人都可以帮我一步一步地实现这一点。我已经检查了https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query#response但我没有收获任何成果。

我的方法

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 = 'any_other_user_email@gmail.com';

$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();

但是我在空闲时间方面变得空白。

1 个答案:

答案 0 :(得分:1)

只需确保日历是公开的,然后尝试google calendar freebusy方法。它肯定会奏效。