带有 PHP API oauth2.0 的 Google 日历服务帐户 - 如何访问未共享的日历 [英] Google Calendar Service Account with PHP API oauth2.0 - How to access calendars that are not shared

查看:46
本文介绍了带有 PHP API oauth2.0 的 Google 日历服务帐户 - 如何访问未共享的日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用 Google 提供的 PHP 库读取日历事件.我想阅读的日历不是公开共享的,但我想在我的服务器应用程序上阅读它们.

I'm having trouble reading calendar events with the PHP libraries provided by Google. The calendars that I want to read are NOT publicly shared, but I want to read them on my server application.

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

The account that holds the calendars is separate from mine (I'll refer to it as the API account), though the calendars are shared with me.

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

According to the not publicly shared Calendar Details on Google:

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

Anyone can: See nothing

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

You can: See only free/busy (hide details).

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

Both the API account and my account have an OAuth2.0 Service Accounts with a P12 key.

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

I've followed a guide at: http://www.daimto.com/google_service_account_php/ which has helped me figure out the authentication process. I've managed to read calendars that are shared publicly without any problems on either account. Here's my code:

// 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
);

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

However, when attempting to use the code for calendars that are not shared publicly I get the following error on either account:

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

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/calendar/v3/calendars{{CalendarID}}:(404) Not Found...

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

When I try var_dump($client->getAuth()), I noticed that there is a value that might suggests that the authentication didn't work:["authenticated":"Google_Client":private]=> bool(false). This happens on both accounts and both publicly shared and publicly not shared calendars.

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

I'm able to use the Google API Explorer with my account to show the JSON data on calendars that aren't shared publicly. Is there something missing with the Google PHP API or Google Account Settings that will allow me to do what I want?

感谢您抽出宝贵时间.

推荐答案

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

If you want to access private calendars with a service account, you will need to either perform an authority delegation if you own the domain containing those calendars (https://developers.google.com/drive/web/delegation describes it for drive but it works for calendar) or you will need to share the private calendar with the email address of the service account.

这篇关于带有 PHP API oauth2.0 的 Google 日历服务帐户 - 如何访问未共享的日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆