0
我探索谷歌日曆API以同步用戶谷歌事件與我的網站events.First我完成與gmail登錄並獲得用戶帳戶離線的權限訪問。我的代碼是網站用戶與他們的Gmail帳戶永久連接如果用戶允許訪問他們的數據
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile(__DIR__.'/client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/google4/oauth2callback.php');
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setIncludeGrantedScopes(true);
//echo $_GET['access_token'].' '.$_GET['code'];
if (!isset($_GET['code']) && !isset($_SESSION['access_token'])) { //echo "werwe";exit;
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$db = mysql_connect('localhost','root','');
mysql_select_db('google',$db);
//echo $_SESSION['refresh_token'];
$client->refreshToken($_SESSION['refresh_token']);
$client->authenticate($_GET['code']);
// $client()->getRefreshToken();
$_SESSION['access_token'] = $client->getAccessToken();
//print_r($_SESSION['access_token']);exit;
//
$sql = "update google set access_token='".$_SESSION['access_token']['refresh_token']."' WHERE userId = '".$_SESSION['userId']."'";
mysql_query($sql);
$_SESSION['refresh_token']=$_SESSION['access_token']['refresh_token'];
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/google4';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
當用戶允許從API訪問他們的數據谷歌返回類似
[
access_token => xxx,
token_type => bearer,
created => xxx,
expires_in => 3600,
refresh_token => xxx
]
陣列使用此訪問令牌我得到的事件數據,我的代碼是
<?php
require_once __DIR__.'/vendor/autoload.php';
//error_reporting(E_PARSE);
session_start();
$client = new Google_Client();
$client->setAuthConfig(__DIR__.'/client_secrets.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/google4/oauth2callback.php');
$client->setAccessType('offline');
echo "<a href='logout.php'> logout </a>";
if (isset($_SESSION['access_token']) && $_SESSION['refresh_token']) {
$client->refreshToken($_SESSION['refresh_token']);
$client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_Calendar($client);
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => date('c'),
);
$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'] . '/google4/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
我得到事件數據但會話過期時如何獲取用戶數據?
我有什麼來存儲用戶,從而使該網站直接連接到他們的帳戶,如果我有脫機訪問權限我可以訪問用戶的日曆數據下線?
如果會話過期使用保存的用戶數據,每次我如何從用戶日曆中獲取數據?
我把這個在我的代碼,但我不知道什麼數據存儲在我的數據庫,所以我可以將它傳遞給谷歌和我從用戶帳戶的事件。 – user3256212
如果您知道任何**演示**代碼,請與我分享鏈接。 – user3256212
抱歉,代碼與另一個項目有關,因此它不可共享。嘗試獲取刷新令牌,您將只能根據範圍訪問這些數據。請不要嘗試獲取用戶不允許的其他數據。 –