我正在尝试使用服务帐户json文件将事件添加到主日历中。
但是,事件未在我的日历中显示,而是在服务帐户日历中显示。我已经定义了范围,但是它不起作用。我必须在其他日历中添加服务日历ID才能查看事件。
ini_set("display_errors", "1");
error_reporting(E_ALL);
session_start();
require dirname(__FILE__).'/gcal/vendor/autoload.php';
$client_id = 'client_id.apps.googleusercontent.com';
$service_account_name = 'serviceaccount.iam.gserviceaccount.com';
$key_file_location = dirname(__FILE__).'/key/key.json';
if (!strlen($service_account_name) || !strlen($key_file_location)) {
echo missingServiceAccountDetailsWarning();
}
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setScopes(["https://www.googleapis.com/auth/calendar"]);
$scope = new Google_Service_Calendar_AclRuleScope();
$scope->setType('user');
$scope->setValue('mygmail@gmail.com');
$rule = new Google_Service_Calendar_AclRule();
$rule->setScope( $scope );
$rule->setRole('owner');
//putenv('GOOGLE_APPLICATION_CREDENTIALS='.dirname(__FILE__).
'/key/key.json');
$client->setAuthConfig($key_file_location);
$client->useApplicationDefaultCredentials();
$service = new Google_Service_Calendar($client);
$result = $service->acl->insert('primary', $rule);
// echo $createdRule->getId(); die;
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
// $key = file_get_contents($key_file_location);
// $cred = new Google_Auth_AssertionCredentials($service_account_name,array('https://www.googleapis.com/auth/calendar'),$key);
// $client->setAssertionCredentials($cred);
// if ($client->getAuth()->isAccessTokenExpired()) {
// $client->getAuth()->refreshTokenWithAssertion($cred);
// }
$_SESSION['service_token'] = $client->getAccessToken();
$event = new Google_Service_Calendar_Event();
$event->setSummary("WHY IS THIS NOT WOKING?");
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime(date('Y-m-d\TH:i:s',strtotime('2014-12-24 10:00:00')));
$start->setTimeZone('Australia/Melbourne');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime(date('Y-m-d\TH:i:s',strtotime('2014-12-26 12:00:00')));
$end->setTimeZone('Australia/Melbourne');
$event->setEnd($end);
$newEvent = $service->events->insert('primary', $event);
$newEvent->getId();
$event = $service->events->get('primary', $newEvent->getId());
echo '<pre>'; print_r($event); echo '</pre>';