我正在尝试访问并编写我的虚拟日历,以使网站具有新功能,但是某些原因使我无法对虚拟日历进行更改。
到目前为止,当我尝试使用此代码时:
$event = $calendar->calendars->get(xxxxxx.com_dsjiub0krm61i5vu1mvee8rh9o@group.calendar.google.com);
结果类似于Google日历的API浏览。
我的日历API
$calendarId2 = 'xxxxxx.com_dsjiub0krm61i5vu1mvee8rh9o@group.calendar.google.com';
$jobname = "Tesla";
$joblocation = "USA";
$jobdescription = "Interview with Elon Musk.";
$startofjob = "2019-03-29T10:00:00.000+00:00";
$endofjob = "2019-03-30T18:00:00.000+00:00";
try {
$client = GetGoogleClient();
$calendar = new \Google_Service_Calendar($client);
$event = new \Google_Service_Calendar_Event(array(
'summary' => $jobname,
'location' => $joblocation,
'description' => $jobdescription,
'end' => array(
'dateTime' => $endofjob,
'timeZone' => 'Asia/Manila',
),
'start' => array(
'dateTime' => $startofjob,
'timeZone' => 'Asia/Manila',
),
));
$event = $calendar->events->insert($calendarId2, $event);
}
catch(\Exception $e) {
return ["error" => $e->getMessage()];
}
该消息将始终显示“您需要对此日历具有作者权限”。即使我已将日历与Make changes to events
或Make changes and manage sharing
的权限共享给服务帐户
其他信息
function GetGoogleClient(){
$client = new Google_Client();
//$client->setAuthConfig($credentialsFile);
$client->addScope("https://www.googleapis.com/auth/drive", Google_Service_Sheets::SPREADSHEETS);
$client->addScope("https://www.googleapis.com/auth/calendar");
//$client->addScope("https://www.googleapis.com/auth/calendar.events"); //same error result event with or without
$client->setAuthConfig(CONST_GOOGLE_SERVICE_ACCOUNT_KEY);
$client->setSubject(CONST_GOOGLE_SERVICE_ACCOUNT_EMAIL);
$client->useApplicationDefaultCredentials();
return $client;
}
答案 0 :(得分:0)
“您需要对此日历具有作者访问权。”
您可能已经与服务帐户共享了它,但是您是否授予它写访问权限?还是只是阅读权限?我认为您应该仔细检查这通常是问题的原因。请记住,这只会授予它访问此日历的权限。
我还认为您应该仅使用
来检查您的范围$client->addScope("https://www.googleapis.com/auth/calendar");
我认为他们正在互相破坏。退后一步以确保日历有效,然后添加驱动器内容
答案 1 :(得分:0)
在Google管理控制台中检查设置是否正确。您无法在单个日历上更改设置的原因之一是,更改的能力已在整个域范围内受到限制。
答案 2 :(得分:0)
如果您想在 G Suite 组织中使用服务帐号,您必须:
这是一个示例代码:
<?php
## composer require google/apiclient:^2.0
require __DIR__ . '/vendor/autoload.php';
/* the path of the auth file downloaded from google */
const AUTH_JSON_FILE = 'service-account-test-12345678.json';
/* the subject to impersonate */
const SUBJECT_TO_IMPERSONATE = 'acmeuser@mydomain.com';
/* take calendar id from calendar settings > embed calendar (the default calendar has the same as SUBJECT_TO_IMPERSONATE */
const CALENDAR_ID = "a_abcde123456fghijk789....@group.calendar.google.com";
/* date of the test event*/
const TEST_EVENT_DATE = '2021-01-26';
function getClient() {
$googleClient = new Google_Client();
$googleClient->setApplicationName("Anything you want");
$googleClient->setAuthConfig(AUTH_JSON_FILE);
$googleClient->setScopes([Google_Service_Calendar::CALENDAR]);
$googleClient->setSubject(SUBJECT_TO_IMPERSONATE);
return $googleClient;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar($client);
/* Create a test event on 2021-01-26 */
$event = new Google_Service_Calendar_Event();
$event->setSummary("Test event title");
$event->setDescription("Test description.....");
$start = new Google_Service_Calendar_EventDateTime();
$start->setDate(TEST_EVENT_DATE);
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDate(TEST_EVENT_DATE);
$event->setEnd($end);
$createdEvent = $service->events->insert(CALENDAR_ID, $event);
echo $createdEvent->getId();