服务帐户无法查看与其明确共享的展示日历

时间:2015-10-05 08:29:27

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

我正在尝试从服务帐户访问所有组织日历,但日历/事件无法查看 - 即使它们已与服务帐户明确共享。

我已经关注了delegation instructions。和Drive example for instantiating a service(转换为日历服务)。

我很惊讶服务帐户无法自动拥有管理员级别的日历访问权限,但即使是显式共享也无法提供所需的访问权限。

如果有人发现了我错过或误解的内容,请告诉我。任何帮助将不胜感激。

麦克

set_include_path(get_include_path() . PATH_SEPARATOR . 'google-api-php-    client-master/src');
require_once 'google-api-php-client-master/src/Google/autoload.php'; // or wherever autoload.php is located

require_once "google-api-php-client/src/Google/Client.php";
require_once "google-api-php-client/src/Google/Service/Calendar.php";
require_once "google-api-php-client/src/Google/Service/Oauth2.php";

define('CALENDAR_SCOPE', 'https://www.googleapis.com/auth/calendar.readonly');
define('SERVICE_ACCOUNT_EMAIL', 'DEV_EMAIL@developer.gserviceaccount.com');
define('SERVICE_ACCOUNT_PKCS12_FILE_PATH', 'P12_FILE.p12');

function buildService($userEmail) {
  $key = file_get_contents(SERVICE_ACCOUNT_PKCS12_FILE_PATH);
  $auth = new Google_Auth_AssertionCredentials(
      SERVICE_ACCOUNT_EMAIL,
      array(CALENDAR_SCOPE),
      $key);
  $auth->sub = $userEmail;
  $client = new Google_Client();
  $client->setAssertionCredentials($auth);
  return new Google_Service_Calendar($client);
}

$v_calendarId_primary = "USER_CHOSEN@email.com";

$service = buildService($v_calendarId_primary);

$calendarId = 'primary';
$optParams = array(
  'maxResults' => 10,
  'orderBy' => 'startTime',
  'singleEvents' => TRUE,
  'timeMin' => date('c'),
);

$results = $service->events->listEvents($v_calendarId_primary, $optParams);

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

1 个答案:

答案 0 :(得分:0)

尝试在USER_CHOSEN@email.com上执行calendar.get。这将确保您可以自己访问日历。然后仔细检查您为服务帐户授予的权限。至少您需要查看所有活动详情。

enter image description here

你的代码看起来很好我只是觉得你没有访问权。