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

查看:91
本文介绍了带有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"与 消息调用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]=> bool(false).这既在帐户上发生,也发生在公共共享日历和公共不共享日历上.

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天全站免登陆