【发布时间】:2020-02-14 23:08:59
【问题描述】:
我正在编写一个 cron 脚本来每天检查我的 Google 日历。 我想知道是否可以使用特定于应用程序的密码 (见:https://support.google.com/accounts/answer/185833?hl=it) 并在我的脚本中插入生成的密码。 OAUTH 需要用户交互,因为我正在编写一个脚本,所以我不能按照这种方式进行操作。 我还阅读了有关“服务帐户”的信息,但希望我可以通过使用特定于应用程序的密码来避免它。有什么不同?有什么提示吗?
非常感谢 弗朗切斯科
EDIT1:我正在尝试的代码:
<?php
require __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
//The json file you got after creating the service account
putenv('GOOGLE_APPLICATION_CREDENTIALS=test-calendario-268115-5452ff6f57e8.json');
$client->useApplicationDefaultCredentials();
$client->setApplicationName("test_calendar");
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
EDIT2: $service->calendarList->listCalendarList() 使用空列表:
<?php
require __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
//The json file you got after creating the service account
putenv('GOOGLE_APPLICATION_CREDENTIALS=test-calendario-268115-5452ff6f57e8.json');
$client->useApplicationDefaultCredentials();
$client->setApplicationName("test_calendar");
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');
$service = new Google_Service_Calendar($client);
$listEvents = $service->events->listEvents("...@group.calendar.google.com");// taken from sharing calendar settings
$events = $listEvents->getItems();
print_r($events);
【问题讨论】:
-
您是否启用了两步验证?
-
是的,我愿意...我已经按照链接中的说明生成了一个新的 16 字符密码
-
服务帐户是为服务器-服务器交互而设计的。我相信这是你的情况,(cron-Calendar API)。阅读此Using OAuth 2.0 for Server to Server Applications
标签: google-api google-calendar-api google-oauth google-api-php-client