title: Google Calendar API - get events
body: <p>I'm using PHP and MySQL but I guess this question can be for almost anyone. I have a website where people can register/create an account and login. I have a calendar on my site for each user. I'm wanting to implement Google Calendar API (oauth) so they can login to their Google accounts and sync their Google Calendar events with the calendar on my site.</p>
  
  <p>But through their documentation, I don't see anywhere that says how I can save the google account for each person on my site so that only their google account gets connected and that only their google calendar events go into their calendar on my site.</p>
  
  <p>For example, if user 2 on my site links their google account (user2@gmail.com) through oauth, how does my site know next time it wants to get events from the google account to get it from user2@gmail.com and not another user's google account that have signed in? Here's what I'm trying to say:</p>
  
  <pre><code>user 2 = user2@gmail.com
  user 5 = user5@gmail.com
  user 8 = user8@gmail.com
  </code></pre>
  
  <p>so next time a user fetches events from Google calendar API, it will get it from their own google account and not another user's g account.</p>
  
  <p>how should I go about doing this? Should I save something in my MySQL database?</p>
  
  <p>Here is the full google calendar api v3 working code:</p>
  
  <p><strong>link-google-account.php:</strong></p>
  
  <pre><code>&lt;?php
  require_once __DIR__.'/vendor/autoload.php';
  
  session_start();
  
  $client = new Google_Client();
  $client-&gt;setAuthConfig('client_secrets.json');
  $client-&gt;addScope(Google_Service_Calendar::CALENDAR_READONLY);
  
  if (isset($_SESSION['access_token']) &amp;&amp; $_SESSION['access_token']) {
    $client-&gt;setAccessToken($_SESSION['access_token']);
  
    // Print the next 10 events on the user's calendar.
    $calendarId = 'primary';
    $optParams = array(
      'maxResults' =&gt; 10,
      'orderBy' =&gt; 'startTime',
      'singleEvents' =&gt; TRUE,
      'timeMin' =&gt; date('c'),
    );
  
    $service = new Google_Service_Calendar($client);
    $results = $service-&gt;events-&gt;listEvents($calendarId, $optParams);
  
    if (count($results-&gt;getItems()) == 0) {
      print "No upcoming events found.\n";
    } else {
      print "Upcoming events:\n";
      foreach ($results-&gt;getItems() as $event) {
        $start = $event-&gt;start-&gt;dateTime;
        if (empty($start)) {
          $start = $event-&gt;start-&gt;date;
        }
        printf("%s (%s)\n", $event-&gt;getSummary(), $start);
      }
    }
  
  } else {
    $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth.php';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  }
  ?&gt;
  </code></pre>
  
  <p><strong>oauth.php:</strong></p>
  
  <pre><code>&lt;?php
  require_once __DIR__.'/vendor/autoload.php';
  
  session_start();
  
  $client = new Google_Client();
  $client-&gt;setAuthConfigFile('client_secrets.json');
  $client-&gt;setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth.php');
  $client-&gt;addScope(Google_Service_Calendar::CALENDAR_READONLY);
  
  if (! isset($_GET['code'])) {
    $auth_url = $client-&gt;createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
  } else {
    $client-&gt;authenticate($_GET['code']);
    $_SESSION['access_token'] = $client-&gt;getAccessToken();
    $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  }
  ?&gt;
  </code></pre>