0

我目前正在使用 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));

如何检索选定的用户帐户(用户会看到一个屏幕,他可以在其中选择他拥有的用于此应用程序的 Google 帐户之一)并使用它而不是设置的“phpkay@gmail.com”帐户?

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

4

1 回答 1

0

You can do this with the OAuth 2.0 API. Just include:

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

in your list of OAuth scopes and then make an authenticated GET request to:

https://www.googleapis.com/oauth2/v2/userinfo

this will return the email address of the authenticated user. You can try this out in the Google API Explorer

于 2013-05-22T14:38:47.867 回答