title: Oauth 2 $client->getAccessToken() Returns Null value
body: <p>I need your help.i am using codeigniter MVC framework for signing in to my site using google client library. every thing is working fine expect $client->getAccessToken() when google redirects with code and i do the following code. $client->getAccessToken() return null value. and i saw many tutorials but didn't get any help. here is my code for controller function one. In this function i set my credentials to create authUrl.</p>
  
  <pre><code>public function login()
  {
      // Include two files from google-php-client library in controller
      include_once APPPATH . 'third_party/google-api-php-client/vendor/autoload.php';
  
      // Store values in variables from project created in Google Developer Console
      $client_id = 'XXXXXX';
      $client_secret = 'XXXXX';
      $redirect_uri = 'path/to/mysite/login/loginGoogle';
      $simple_api_key = 'XXXXXXX';
  
      // Create Client Request to access Google API
      $client = new Google_Client();
      $client-&gt;setApplicationName("mysite");
      $client-&gt;setClientId($client_id);
      $client-&gt;setClientSecret($client_secret);
      $client-&gt;setRedirectUri($redirect_uri);
      $client-&gt;setDeveloperKey($simple_api_key);
      $client-&gt;addScope("https://www.googleapis.com/auth/userinfo.email");
  
      $authUrl = $client-&gt;createAuthUrl();
      $data['authUrl'] = $authUrl;
  
      $this-&gt;load-&gt;view('login',$data);
  }
  </code></pre>
  
  <p>And after that when google authenticate and redirects to my redirect uri which is an other controller function which is given below. and problem is in this function.</p>
  
  <pre><code>public function loginGoogle()
  {
      // Include two files from google-php-client library in controller
      include_once APPPATH . 'third_party/google-api-php-client/vendor /autoload.php';
         $client_id = 'XXXXXX';
          $client_secret = 'XXXXX';
          $redirect_uri = 'path/to/mysite/login/loginGoogle';
          $simple_api_key = 'XXXXXXX';
  
      // Create Client Request to access Google API
      $client = new Google_Client();
      $client-&gt;setApplicationName("mysite");
      $client-&gt;setClientId($client_id);
      $client-&gt;setClientSecret($client_secret);
      $client-&gt;setRedirectUri($redirect_uri);
      $client-&gt;setDeveloperKey($simple_api_key);
      $client-&gt;addScope("https://www.googleapis.com/auth/userinfo.email");
  $objOAuthService = new Google_Service_Oauth2($client);
  
      // Add Access Token to Session
      if(!isset($_SESSION['access_token'])){
  
          if (isset($_GET['code'])) {
              $client-&gt;authenticate($_GET['code']);
              $token = $client-&gt;getAccessToken();                 
              $_SESSION['access_token'] = $token;
              print_r($this -&gt; session -&gt; userdata());exit;
              header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
          }
      }
      // Set Access Token to make Request
      if (isset($_SESSION['access_token']) &amp;&amp; $_SESSION['access_token']) {
          $client-&gt;setAccessToken($_SESSION['access_token']);
      }
      // Get User Data from Google and store them in $data
      if ($client-&gt;getAccessToken()) {
          $userData = $objOAuthService-&gt;userinfo-&gt;get();
          $data['userData'] = $userData;
          $_SESSION['access_token'] = $client-&gt;getAccessToken();
      }}
  </code></pre>
  
  <p>here in second function getAccessToken return nothing and google throws expection. Please help me out. </p>