网站用户永久连接其Gmail帐户如果允许用户访问其数据

时间:2016-10-20 06:19:48

标签: php google-api google-calendar-api google-api-php-client google-api-client

我正在探索google calendar api,以便将用户google事件与我的网站事件同步。首先,我完成了使用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 google访问他们的数据时返回数组

[
  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));
}

我获取了事件数据,但是当会话到期时如何获取用户数据?

我必须为该用户存储什么,以便我可以离线访问用户日历数据,以便网站直接连接到他们的帐户,如果我有离线访问权限?

如果会话使用保存的用户数据到期,我每次如何从用户日历中获取数据?

1 个答案:

答案 0 :(得分:1)

Google允许离线访问60天(请确保来自google.com的最新政策) 要获得离线访问权限,您需要在oAuth请求中传递以下标志

access_type=offline

请注意,如果您想强制要求离线访问用户

,则以下标志非常有用
approval_prompt=force

因此,每次您需要访问Google数据时,您需要使用以前的访问令牌数据请求刷新令牌。您必须将访问令牌数据保存到数据库中。