title: Get logged in user with google client api php
body: <p>I have an app set up and working for users to insert events in their calendars. I need to verify if they logged in with the correct google account. (work email). I have searched the web for hours with no luck. I've tried several different things with no luck :/ I hope someone can guide me or help me get this to work. Thank you! Here is what I have</p>
  
  <pre><code>$client = new Google_Client();
  $client-&gt;setApplicationName("Intakes Calendar");
  $client-&gt;setClientId('xxxxxxxx.apps.googleusercontent.com');
  $client-&gt;setClientSecret('xxxxx');
  $client-&gt;setRedirectUri('xxx');
  $client-&gt;setDeveloperKey('xxx');
  $client-&gt;setScopes(array('https://www.googleapis.com/auth/calendar'));
  $client-&gt;addScope('https://www.googleapis.com/auth/userinfo.email');
  
  if (isset($_GET['code'])) {
      $client-&gt;authenticate($_GET['code']);
      $_SESSION['token'] = $client-&gt;getAccessToken();
  }
  
  if (isset($_SESSION['token'])) {
      //echo "&lt;br&gt;Getting access";
      $client-&gt;setAccessToken($_SESSION['token']);
  }
  
  if ($client-&gt;getAccessToken()){
  
      if($client-&gt;isAccessTokenExpired()) {
  
          $authUrl = $client-&gt;createAuthUrl();
          $button =  '&lt;a class="btn btn-success btn-sm" href="'.$authUrl.'"&gt;SIGN IN&lt;/a&gt;';
          $msg = '&lt;DIV class="col-md-6 col-md-offset-3 alert alert-warning"&gt;You are not signed in with your work email account for remote sessions.' . $button . '&lt;/DIV&gt;';
  
      }else{
  
          //if logged in check for user email 
          if($_SESSION['USER_EMAIL'] == 'GOOGLE LOGGED IN EMAIL?')
              $msg = ' &lt;DIV class="col-md-6 col-md-offset-3 alert alert-warning"&gt;You are connected and ready for remote sessions.&lt;/DIV&gt;';
          else
              $msg = ' &lt;DIV class="col-md-6 col-md-offset-3 alert alert-warning"&gt;Please log in with your work email.&lt;/DIV&gt;';
      }
  
  }else{
  
      $authUrl = $client-&gt;createAuthUrl();
      $button =  '&lt;a class="btn btn-success btn-sm" href="'.$authUrl.'"&gt;SIGN IN&lt;/a&gt;';
      $msg = '&lt;DIV class="col-md-6 col-md-offset-3 alert alert-warning"&gt;You are not signed in with your work email account for remote sessions.' . $button . '&lt;/DIV&gt;';
  
  }
  </code></pre>