Google日历如何解决“您需要对此日历具有作者访问权限”?

时间:2019-03-28 12:59:45

标签: php google-calendar-api slim

我正在尝试访问并编写我的虚拟日历,以使网站具有新功能,但是某些原因使我无法对虚拟日历进行更改。

到目前为止,当我尝试使用此代码时:

$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 eventsMake 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;
}

3 个答案:

答案 0 :(得分:0)

  

“您需要对此日历具有作者访问权。”

您可能已经与服务帐户共享了它,但是您是否授予它写访问权限?还是只是阅读权限?我认为您应该仔细检查这通常是问题的原因。请记住,这只会授予它访问此日历的权限。

enter image description here

我还认为您应该仅使用

来检查您的范围
$client->addScope("https://www.googleapis.com/auth/calendar");

我认为他们正在互相破坏。退后一步以确保日历有效,然后添加驱动器内容

答案 1 :(得分:0)

在Google管理控制台中检查设置是否正确。您无法在单个日历上更改设置的原因之一是,更改的能力已在整个域范围内受到限制。

答案 2 :(得分:0)

如果您想在 G Suite 组织中使用服务帐号,您必须:

  1. 使用 Gsuite 普通帐户(也不是管理员)登录
  2. 创建 API 项目
  3. 启用日历 API
  4. 管理凭据并添加服务帐号
  5. 下载服务帐号的json授权文件,复制json文件中的client_id
  6. 从创建服务帐户的帐户在控制台中为服务帐户启用域范围的委派。
  7. 启用日历 API
  8. 使用域管理员帐户登录并转到 Google 管理控制台,然后转到安全 -> API 控制 -> 域范围委派
  9. 然后添加新的 API 客户端,在第 4 点插入 client_id 和范围 https://www.googleapis.com/auth/calendar(或其中之一:calendar.events、calendar.events.readonly、calendar.readonly、calendar.settings.readonly)
  10. 现在该服务帐户可以冒充任何人。

这是一个示例代码:

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