title: PHP : Google plus Oauth 2.0 - Getting Error fetching OAuth2 access token, message: 'invalid_client'
body: <p>Working on <em>Sign in with Google</em>, using <em>PHP Lib v2, Oauth 2.0</em>. </p>
  
  <p>After long time spending on it finally I configured, it worked once fine but after that it is not working. On the clicking of sign in link, it will take user for authentication to Google. Working fine till then authentication done.</p>
  
  <p>At the time of callback with <code>GET</code> variables:</p>
  
  <pre><code>?code=something&amp;authuser=0&amp;prompt=consent&amp;session_state=something
  </code></pre>
  
  <p>It is generating following error:</p>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'Google_AuthException' with message
    'Error fetching OAuth2 access token, message: 'invalid_client'' in
    C:\xampp\htdocs\test\google-api-php-client\src\auth\Google_OAuth2.php:115
    Stack trace: #0
    C:\xampp\htdocs\test\google-api-php-client\src\Google_Client.php(127):
    Google_OAuth2->authenticate(Array, 4/xUGSPbXR0LNzW...') #1
    C:\xampp\htdocs\test\google-api-php-client\index.php(40):
    Google_Client->authenticate('4/xUGSPbXR0LNzW...') #2 {main} thrown in
    C:\xampp\htdocs\test\google-api-php-client\src\auth\Google_OAuth2.php
    on line 115</p>
  </blockquote>
  
  <p>Using following code (it will help you to understand):</p>
  
  <pre><code>&lt;?php
  ini_set('display_errors',1);
  error_reporting(E_ALL);
  
  require_once 'src/Google_Client.php';
  require_once 'src/contrib/Google_PlusService.php';
  
  session_start();
  
  $client = new Google_Client();
  $client-&gt;setApplicationName("Google+ PHP Starter Application");
  $plus = new Google_PlusService($client);
  
  if (isset($_REQUEST['logout'])) {
    unset($_SESSION['access_token']);
  }
  
  if (isset($_GET['code'])) {
    $client-&gt;authenticate($_GET['code']);
    $_SESSION['access_token'] = $client-&gt;getAccessToken();
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
  }
  
  if (isset($_SESSION['access_token'])) {
    $client-&gt;setAccessToken($_SESSION['access_token']);
  }
  
  if ($client-&gt;getAccessToken()) {
    $me = $plus-&gt;people-&gt;get('me');
    $url = filter_var($me['url'], FILTER_VALIDATE_URL);
    $img = filter_var($me['image']['url'], FILTER_VALIDATE_URL);
    $name = filter_var($me['displayName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $personMarkup = "&lt;a rel='me' href='$url'&gt;$name&lt;/a&gt;&lt;div&gt;&lt;img src='$img'&gt;&lt;/div&gt;";
  
    $optParams = array('maxResults' =&gt; 100);
    $activities = $plus-&gt;activities-&gt;listActivities('me', 'public', $optParams);
    $activityMarkup = '';
    foreach($activities['items'] as $activity) {
      $url = filter_var($activity['url'], FILTER_VALIDATE_URL);
      $title = filter_var($activity['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
      $content = filter_var($activity['object']['content'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
      $activityMarkup .= "&lt;div class='activity'&gt;&lt;a href='$url'&gt;$title&lt;/a&gt;&lt;div&gt;$content&lt;/div&gt;&lt;/div&gt;";
    }
  
    $_SESSION['access_token'] = $client-&gt;getAccessToken();
  } else {
    $authUrl = $client-&gt;createAuthUrl();
  }
  ?&gt;
  &lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;link rel='stylesheet' href='style.css' /&gt;
  &lt;body&gt;
  &lt;header&gt;&lt;h1&gt;Google+ Sample App&lt;/h1&gt;&lt;/header&gt;
  &lt;div class="box"&gt;
  
  &lt;?php if(isset($personMarkup)): ?&gt;
  &lt;div class="me"&gt;&lt;?php print $personMarkup ?&gt;&lt;/div&gt;
  &lt;?php endif ?&gt;
  
  &lt;?php if(isset($activityMarkup)): ?&gt;
  &lt;div class="activities"&gt;Your Activities: &lt;?php print $activityMarkup ?&gt;&lt;/div&gt;
  &lt;?php endif ?&gt;
  
  &lt;?php
    if(isset($authUrl)) {
      print "&lt;a class='login' id='g_pop' href='$authUrl'&gt;Connect Me!&lt;/a&gt;";
    } else {
        print_r($me);
     print "&lt;a class='logout' href='?logout'&gt;Logout&lt;/a&gt;";
    }
  ?&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Get source files from here: <s><a href="http://code.google.com/p/google-api-php-client/downloads/list" rel="nofollow">Google APIs Client Library for PHP</a></s></p>
  
  <p>Already done:</p>
  
  <ul>
  <li>Client ID:   OK </li>
  <li>Client secret: OK</li>
  <li>Redirect URIs: OK</li>
  <li>Google+ API: ON</li>
  </ul>
  
  <p><strong>Note:</strong></p>
  
  <p>Working on localhost.</p>