我正在使用php和mysql,但我想这个问题几乎适用于任何人。我有一个网站,人们可以注册/创建一个帐户和登录。我的网站上有每个用户的日历。我想实现googlecalendarapi(oauth),这样他们就可以登录到他们的google帐户,并将他们的google日历事件与我网站上的日历同步。
但通过他们的文档,我看不到任何地方说,我可以保存谷歌帐户为每个人在我的网站上,只有他们的谷歌帐户得到连接,只有他们的谷歌日历事件进入他们的日历在我的网站上。
例如,如果我网站上的用户2链接了他们的google帐户(user2@gmail.com)通过oauth,我的站点如何知道下次它想从google帐户获取事件来获取它呢user2@gmail.com 而不是其他用户的谷歌帐户已经登录?我想说的是:
user 2 = user2@gmail.com
user 5 = user5@gmail.com
user 8 = user8@gmail.com
因此,下次用户从google日历api获取事件时,它将从自己的google帐户而不是其他用户的g帐户获取。
我该怎么做呢?我应该在mysql数据库中保存一些东西吗?
以下是完整的google calendar api v3工作代码:
link-google-account.php:
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => date('c'),
);
$service = new Google_Service_Calendar($client);
$results = $service->events->listEvents($calendarId, $optParams);
if (count($results->getItems()) == 0) {
print "No upcoming events found.\n";
} else {
print "Upcoming events:\n";
foreach ($results->getItems() as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
printf("%s (%s)\n", $event->getSummary(), $start);
}
}
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
oauth.php文件:
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth.php');
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
暂无答案!
目前还没有任何答案,快来回答吧!