Google API:无人值守的日历读取.如何登录


Google API : Unattended reading of calendar. How to login?

我以为这个会很轻而易举。因此,我被困在一开始:-(

我创建了一个Google日历,用户可以在其中使用"共享"专用登录来添加事件。然后我需要在我的服务器上编写一个 PHP 脚本来读取给定时间范围内的事件。

我认为这里描述的 API 密钥就足够了。但事实并非如此。

 curl https://www.googleapis.com/calendar/v3/users/me/calendarList?key=mykey

Login required说。

所以我阅读了有关OAuth2.0以及我需要用户如何进行身份验证的信息。问题是我的脚本不是交互式的(尽管在脚本中硬编码登录对我来说不是问题:信息不是生命攸关的(。所以我阅读了有关服务帐户的信息,但看起来它是针对非用户信息的。

问:如何编写脚本以强制给定登录而不涉及人工?

注意:这个SO问题似乎很有希望,但答案是针对API版本2.0的,它似乎已经过时了。

您需要做的第一件事是获取访问令牌。这将需要一个人。假设您使用的是Google APIs PHP Client,则可以使用此脚本(从命令行运行(来完成此操作。

注意:下面的代码片段适用于已安装应用程序的客户端 ID。请确保在 Google API 访问控制台中创建此类型的客户端 ID。

require_once '../../src/apiClient.php';
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
$client = new apiClient();
// Visit https://code.google.com/apis/console to create your client id and cient secret
$client->setClientId('INSERT_CLIENT_ID');
$client->setClientSecret('INSERT_CLIENT_SECRET');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array(
    'https://www.googleapis.com/auth/calendar',
    'https://www.googleapis.com/auth/calendar.readonly',
));
$authUrl = $client->createAuthUrl();
print "Please visit:'n$authUrl'n'n";
print "Please enter the auth code:'n";
$authCode = trim(fgets(STDIN));
$_GET['code'] = $authCode;
$token = $client->authenticate();
var_dump($token);

这将为您提供一个包含访问令牌和刷新令牌的 json 编码字符串。访问令牌会在 1 小时后过期,但不要担心。刷新令牌不会过期(除非您卸载应用程序(,并且可用于获取新的刷新令牌。客户端库负责此部分。

接下来,将完整的 json 字符串令牌(不仅是 access_token 属性(保存在安全的位置,并确保其他人无法读取它。然后,你的应用可以调用从安全位置查找$token的$client->setAccessToken($token)(同样,$token是完整的 json 编码字符串,不限于其 access_token 属性(。

现在,您可以向日历 API 发出经过身份验证的请求!

require_once '../../src/apiClient.php';
require_once '../../src/contrib/apiCalendarService.php';
session_start();
$client = new apiClient();
$client->setApplicationName("Google Calendar PHP Sample Application");
$cal = new apiCalendarService($client);
$client->setAccessToken($tokenFromSafePlace);
$calList = $cal->calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";

使用最新版本的客户端库(适用于 v3 api?(获取所需的令牌:

require_once 'Google/Client.php';
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
$client = new Google_Client();
// Visit https://console.developers.google.com/ to create your application and client id for a native app.
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array(
    'https://www.googleapis.com/auth/calendar',
    'https://www.googleapis.com/auth/calendar.readonly',
));
$authUrl = $client->createAuthUrl();
print "Please visit:'n$authUrl'n'n";
print "Please enter the auth code:'n";
$authCode = trim(fgets(STDIN));
$_GET['code'] = $authCode;
$token = $client->authenticate($authCode);
var_dump($token);

若要获取日历列表,则需要以下代码:

require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';
$client = new Google_Client();
$client->setApplicationName("My Calendar example");
// token you got from the previous code
$client->setAccessToken($token);
$calendarService = new Google_Service_Calendar($client);
$calendarList = $calendarService->calendarList;
$calList = $calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";