title: Logging in with Google Oauth in PHP without authenticate permission
body: <p>Am using <a href="https://github.com/googleapis/google-api-php-client/releases" rel="nofollow noreferrer">https://github.com/googleapis/google-api-php-client/releases</a> </p>
  
  <p>Source File :<a href="https://github.com/googleapis/google-api-php-client/archive/v2.4.0.zip" rel="nofollow noreferrer">https://github.com/googleapis/google-api-php-client/archive/v2.4.0.zip</a></p>
  
  <p>to get User details like <code>Username, email, profile picture, user ID, access token, refresh token so on</code> and i was able to get all details with user authenticate permission and saved it to my database.</p>
  
  <p><strong>Google_config.php</strong></p>
  
  <pre><code>require_once 'Google/autoload.php';
  //require_once 'client.json';
  
  session_start();
  
  $client = new Google_Client();
  $client-&gt;setApplicationName("Login with Google Account");
  
  $client-&gt;setClientId('xxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com');
  $client-&gt;setClientSecret('xxxxxxxxxxxxxxxxxxxxx');
  $client-&gt;setRedirectUri('http://localhost:8000/ads/login/redirect.php');
  
  //$client-&gt;setAuthConfig("client.json");
  
  $client-&gt;addScope([
      "https://www.googleapis.com/auth/userinfo.email",
      "https://www.googleapis.com/auth/userinfo.profile"
  ]);
  $client-&gt;setAccessType('offline');
  $client-&gt;setApprovalPrompt ("force");
  </code></pre>
  
  <p><strong>Redirect.php</strong></p>
  
  <pre><code>if (isset($_SESSION['accessToken'])) {
      $client-&gt;setAccessToken($_SESSION['accessToken']);
  } else if (isset($_GET['code'])) {
      $client-&gt;authenticate($_GET['code']);
      $access_token = $client-&gt;getAccessToken();
      $_SESSION['accessToken'] = $access_token;
  } else {
      header('location : index.php');
  }
  
  $oauth = new Google_Service_Oauth2($client);
  
  
  if ($client-&gt;getAccessToken()) {
      // Get user profile data from google
      $gpUserProfile = $oauth-&gt;userinfo-&gt;get();
  
      // Initialize User class
      $user = new User();
  
      // Getting user profile info
      $gpUserData = array();
      $gpUserData['oauth_uid'] = !empty($gpUserProfile['id']) ? $gpUserProfile['id'] : '';
      $gpUserData['first_name'] = !empty($gpUserProfile['given_name']) ? $gpUserProfile['given_name'] : '';
      $gpUserData['last_name'] = !empty($gpUserProfile['family_name']) ? $gpUserProfile['family_name'] : '';
      $gpUserData['email'] = !empty($gpUserProfile['email']) ? $gpUserProfile['email'] : '';
      $gpUserData['gender'] = !empty($gpUserProfile['gender']) ? $gpUserProfile['gender'] : '';
      $gpUserData['locale'] = !empty($gpUserProfile['locale']) ? $gpUserProfile['locale'] : '';
      $gpUserData['picture'] = !empty($gpUserProfile['picture']) ? $gpUserProfile['picture'] : '';
      $gpUserData['link'] = !empty($gpUserProfile['link']) ? $gpUserProfile['link'] : '';
  
      // Insert or update user data to the database
      $gpUserData['oauth_provider'] = 'google';
      $userData = $user-&gt;checkUser($gpUserData);
  
      // Storing user data in the session
      $_SESSION['userData'] = $userData;
  
      // Render user profile data
      if (!empty($userData)) {
          $output = '&lt;h2&gt;Google Account Details&lt;/h2&gt;';
          $output .= '&lt;div class="ac-data"&gt;';
          $output .= '&lt;img src="' . $userData['picture'] . '"&gt;';
          $output .= '&lt;p&gt;&lt;b&gt;Google ID:&lt;/b&gt; ' . $userData['picture'] . '&lt;/p&gt;';
          $output .= '&lt;p&gt;&lt;b&gt;Google ID:&lt;/b&gt; ' . $userData['oauth_uid'] . '&lt;/p&gt;';
          $output .= '&lt;p&gt;&lt;b&gt;Name:&lt;/b&gt; ' . $userData['first_name'] . ' ' . $userData['last_name'] . '&lt;/p&gt;';
          $output .= '&lt;p&gt;&lt;b&gt;Email:&lt;/b&gt; ' . $userData['email'] . '&lt;/p&gt;';
          $output .= '&lt;p&gt;&lt;b&gt;Gender:&lt;/b&gt; ' . $userData['gender'] . '&lt;/p&gt;';
          $output .= '&lt;p&gt;&lt;b&gt;Locale:&lt;/b&gt; ' . $userData['locale'] . '&lt;/p&gt;';
          $output .= '&lt;p&gt;&lt;b&gt;access token:&lt;/b&gt; ' . $client-&gt;getAccessToken() . '&lt;/p&gt;';
          $output .= '&lt;p&gt;&lt;a href="' . $userData['link'] . '" target="_blank"&gt;Click to visit Google+&lt;/a&gt;&lt;/p&gt;';
          $output .= '&lt;p&gt;Logout from &lt;a href="logout.php"&gt;Google&lt;/a&gt;&lt;/p&gt;';
          $output .= '&lt;/div&gt;';
      } else {
          $output = '&lt;h3 style="color:red"&gt;Some problem occurred, please try again.&lt;/h3&gt;';
      }
  } else {
      // Get login url
      $authUrl = $client-&gt;createAuthUrl();
  
      // Render google login button
      $output = '&lt;a href="' . filter_var($authUrl, FILTER_SANITIZE_URL) . '"&gt;&lt;img src="images/google-sign-in-btn.png" alt=""/&gt;&lt;/a&gt;';
  }
  ?&gt;
  &lt;div class="container"&gt;
      &lt;!-- Display login button / Google profile information --&gt;
      &lt;?php echo $output; ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Now my problem is <strong>HOW DOES GOOGLE LOGIN WORKS</strong> Because when i use above method it get user <strong>authentication</strong> and then give back user info.</p>
  
  <p>Where here in <a href="https://stackoverflow.com/">https://stackoverflow.com/</a> when i try to log-in i was able to login with google account with some redirect.</p>
  
  <p>How do i do a login, tried many online solution and google document too.</p>
  
  <p>Any solution would be helpfull.</p>