带有PHP和服务帐户的Google Calendar API不再起作用 [英] Google Calendar API with PHP and a Service Account won't work anymore

查看:104
本文介绍了带有PHP和服务帐户的Google Calendar API不再起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大约有15个日历的网站,我可以使用带有PHP和服务帐户的Google Calendar API连接到Google日历.我只使用普通的旧gmail帐户,而不使用gsuite帐户.我使用我的网站在这些日历上添加,编辑和删除事件.

I have a site with approximately 15 calendars that I connect to Google calendars using the Google Calendar API with PHP and a service account. I use just a plain old gmail account not a gsuite account. I use my site to add, edit and delete events on these calendars.

    function Google_Calendar_Connect(){
$client = new Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS=Private_Key.json');
$client->useApplicationDefaultCredentials();
$client->setApplicationName("Mical");
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');

$service = new Google_Service_Calendar($client);
return $service;
}
$service = Google_Calendar_Connect();
$event = $service->events->get($calendarId, $Array["google_id"]);
$event->setSummary($Array2['fullName']);
$event->setLocation($Array['job_address']);
$event->setDescription($Array['description']);
try{
$updatedEvent = $service->events->update($calendarId, $event->getId(), $event);
}catch (Exception $ex)
        {echo $ex;
            // Event was not found on Google Calendar.
        } finally {
//if it worked, do something
}

通常,我会将服务帐户电子邮件添加到日历与人共享"设置对事件进行更改";权限,这允许上面的脚本编辑日历事件.最近,当我在Google上添加新日历并尝试将服务帐户电子邮件添加到与人共享"设置,它不允许我这样做.

Normally I would add the service account email into the calendar "share with people" setting with "Make changes to events" permission, this allows the script above to edit the calendar event. Recently when I added a new calendar on Google and tried to add the service account email to the "share with people" setting, it would not allow me to do so.

我阅读了服务帐户不再接受自动共享的日历,它是由Google修复,因为该服务帐户无法验证或批准自己.无论如何,现在这意味着我无法在此新日历或我创建的任何新日历中添加编辑或删除事件.它仍然可以在与服务帐户共享的旧日历上使用,因为它们仍然可以共享.

I read on Service Accounts don't accept automatically shared calendars anymore that it is a fix by Google because the service account cannot verify or approve itself. Anyway, this now means that I can not add edit or delete events this new calendar or any new calendars that I create. It still works on the old calendars that were shared with the service account because they are still shared.

任何人都可以推荐一种无需用户干预且无需在Gsuite中重新创建新日历的连接日历的修补程序或其他方法吗?

Can anyone recommend a fix or different way of connecting to the calendar without user interaction and without having to recreate new calendars in Gsuite ?

推荐答案

第一步.

我希望您检查问题是否仅仅是您要查找的日历没有出现在日历列表中,正确的是,服务帐户不再自动插入日历列表中,但这并不意味着您没有有权访问日历.

step one.

I would like you to check that the issue isnt simply that the calendar you are looking for is not appearing in your calendarlist, it is correct that service accounts no longer insert automatically into the calendar list but that does not mean that you dont have access to the calendar.

尝试执行类似的操作以将日历添加到日历列表.您应该能够从Google日历网络应用程序中找到日历ID.

Try and do something like this to add the calendar to calendarlist. You should be able to find the calendar id from Google calendar web application.

$calendarListEntry = new Google_Service_Calendar_CalendarListEntry();
$calendarListEntry->setId("calendarId");

$createdCalendarListEntry = $service->calendarList->insert($calendarListEntry);

echo $createdCalendarListEntry->getSummary();

选项二

如果您说您确实再无权访问日历,而问题不仅仅在于它没有为您自动添加到日历列表中.

option two

If as you say that you truly do not have access to the calendar anymore and the issue isn't simply that it wasn't added automatically for you to the calendar list.

唯一的解决方法是创建一个虚拟Google用户,然后使用Oauth2通过脱机访问"对用户进行身份验证,这将为您提供刷新令牌.

The only work around for this would be to create a dummy Google user, then using Oauth2 authenticate the user using Offline access, this will give you a refresh token.

然后,您可以在需要时使用刷新令牌来请求新的访问令牌.使用

You can then use the refresh token to request a new access token when ever you need to. Using

$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());

这不是理想的解决方案,因为刷新令牌可能会停止工作,尽管这种情况极少发生,因此它可能会停止工作,因此您需要先坐下来等待代码,如果刷新令牌有任何问题,它会通知您,以便您可以创建一个新的

This is not an ideal solution as refresh tokens can stop working although it is extremely rare it can happen so you will need to baby sit the code and have it notify you if it has any issues with its refresh token so that you can create a new one

这篇关于带有PHP和服务帐户的Google Calendar API不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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