【问题标题】:Google Calendar Service Account with PHP API oauth2.0 - How to access calendars that are not shared带有 PHP API oauth2.0 的 Google 日历服务帐户 - 如何访问未共享的日历
【发布时间】:2020-02-15 00:13:15
【问题描述】:

我在使用 Google 提供的 PHP 库读取日历事件时遇到问题。我想阅读的日历不是公开共享的,但我想在我的服务器应用程序上阅读它们。

保存日历的帐户与我的帐户是分开的(我将其称为 API 帐户),尽管日历是与我共享的。

根据 Google 上未公开共享的日历详细信息:

任何人都可以:什么都看不见

您可以:仅查看忙/闲(隐藏详细信息)。

API 帐户和我的帐户都有一个带有 P12 密钥的 OAuth2.0 服务帐户。

我遵循了http://www.daimto.com/google_service_account_php/ 的指南,该指南帮助我了解了身份验证过程。我已经设法阅读了公开共享的日历,而这两个帐户都没有任何问题。这是我的代码:

// Start the Google client
$client = new Google_Client();
$client->setClientId({{client_id from google}});
$client->setApplicationName("Project Name");
$client->setClientSecret({{client_secret from google}});
$key = file_get_contents({{key_file_location on my server}});
$cred = new Google_Auth_AssertionCredentials(
        $Email_address,
        // Scopes that we will be using
        array(
           "https://www.googleapis.com/auth/calendar"
        ),
        $key
);

$client->setAssertionCredentials($cred);

if($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}

// Use Google Calendar API plugin
$service = new Google_Service_Calendar($client);

$optParams = array(
    'singleEvents'=> true,
    'timeMin'=>date(DateTime::ATOM),
    'timeMax' => date(DateTime::ATOM, time()+(3 * 24 * 60 * 60))
);

$results = $service->events->listEvents(
    '{{Calendar ID from Google Calendar Settings}}',
    $optParams
);

但是,当尝试将代码用于未公开共享的日历时,我在任一帐户上都收到以下错误:

致命错误:未捕获的异常“Google_Service_Exception”与 message '调用 GET 时出错 https://www.googleapis.com/calendar/v3/calendars{{CalendarID}}:(404) 未找到...

当我尝试var_dump($client->getAuth()) 时,我注意到有一个值可能表明身份验证不起作用:["authenticated":"Google_Client":private]=> bool(false)。这发生在两个帐户以及公开共享和未公开共享的日历上。

我可以通过我的帐户使用 Google API Explorer 在未公开共享的日历上显示 JSON 数据。 Google PHP API 或 Google 帐户设置是否缺少某些东西可以让我做我想做的事?

感谢您的宝贵时间。

【问题讨论】:

    标签: php api google-calendar-api shared


    【解决方案1】:

    如果您想使用服务帐户访问私人日历,如果您拥有包含这些日历的域(https://developers.google.com/drive/web/delegation 将其描述为驱动器,但它适用于日历),则需要执行权限委派,或者您需要与服务帐户的电子邮件地址共享私人日历。

    【讨论】:

      【解决方案2】:

      感谢您的建议。

      原来问题在于 API 帐户的负责人没有与开发者帐户的服务器帐户电子邮件共享日历。

      由于我无权访问 API 帐户设置,而且来自 Google Calendar API 的错误描述性不强,因此需要一段时间才能弄清楚。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多