谷歌日历API——>如何使从中检索事件的用户帐户可变?


Google Calendar API --> How can I make the useraccount from which I retrieve Events, variable?

我目前正在使用Google Calendar API。我从用户的日历中检索EVENTS列表,但此时用户的帐户是在我的代码中给出的。我不想让这个变量(它应该从您选择的帐户检索事件)。

这是我的代码:

<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_CalendarService.php';
$client = new Google_Client();
$client->setApplicationName('Tryout for Google Calendar');
// Visit https://code.google.com/apis/console?api=plus to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('myid');
$client->setClientSecret('mysecret');
$client->setRedirectUri('http://localhost/gcal/done.php');
$client->setDeveloperKey('mykey');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$minCheck = date(DATE_ATOM);
$eventList = $cal->events->listEvents("phpkay@gmail.com", array('singleEvents' =>                      'true', 'timeMin' => $minCheck));
  print "<h1>Calendar List</h1><pre>" . print_r($eventList, true) . "</pre>";

$_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>

这是我设置用户帐户的部分,即:"phpkay@gmail.com"

$eventList = $cal->events->listEvents("phpkay@gmail.com", array('singleEvents' => 'true', 'timeMin' => $minCheck));

我如何检索选定的用户帐户(用户得到一个屏幕,他可以选择一个他拥有的谷歌帐户用于此应用程序),并使用这个,而不是设置"phpkay@gmail.com"帐户?

对不起,如果我的问题不清楚,我不知道如何用另一种方式表达。

您可以使用OAuth 2.0 API完成此操作。包括:

https://developers.google.com/admin-sdk/directory/v1/reference/users

在OAuth作用域列表中,然后向

发出一个经过身份验证的GET请求:
https://www.googleapis.com/oauth2/v2/userinfo

将返回经过身份验证的用户的电子邮件地址。你可以在Google API资源管理器

中尝试一下。