title: Making requests to Google API using Guzzle and Socialite in Laravel 5
body: <p>I'm building a web app that has to interact with Google Contacts API and retrieve the Contact List of the authenticated user, but I'm getting </p>
  
  <pre><code>ClientException in RequestException.php line 89:
  Client error response [url] https://www.google.com/m8/feeds/contacts/ambermphatic@gmail.com/full?prettyPrint=false [status code] 403 [reason phrase] Forbidden
  </code></pre>
  
  <p>Here's my AuthenticateUser.php where I have included the getContactList function, I'm trying to make the Guzzle Request to the Google Server and managed to send the correct access token by storing it in a session variable, but I'm still getting a forbidden response :</p>
  
  <pre><code>&lt;?php
  namespace App;
  use Laravel\Socialite\Contracts\Factory as Socialite;
  use App\Repositories\UserRepository;
  use Illuminate\Contracts\Auth\Guard;
  
  
  class AuthenticateUser {
  
      /**
       * @var UserRepository
       */
      private $users;
      /**
       * @var Socialite
       */
      private $socialite;
      /**
       * @var Guard
       */
      private $guard;
  
      private $token;
  
      public function __construct(UserRepository $users, Socialite $socialite, Guard $guard)
      {
  
          $this-&gt;users = $users;
          $this-&gt;socialite = $socialite;
          $this-&gt;guard = $guard;
      }
  
  
      /**
       * @param $hasCode
       * @param AuthenticateUserListener $listener
       * @return mixed
       */
      public function execute($hasCode, AuthenticateUserListener $listener)
      {
  
          if ( ! $hasCode ) return $this-&gt;getAuthorizationFirst();
  
          $var = $this-&gt;getGoogleUser();
  
          $user = $this-&gt;users-&gt;findByUsernameOrCreate($var);
  
          \Session::put('token', $var-&gt;token );
  
          \Auth::login($user, true);
  
          return $listener-&gt;userHasLoggedIn($user);
  
      }
  
      public function logout()
      {
  
          \Auth::logout();
  
          return redirect('/');
  
  
      }
  
      private function getAuthorizationFirst()
      {
  
          return \Socialize::with('google')-&gt;redirect();
  
      }
  
      private function getGoogleUser()
      {
  
          return \Socialize::with('google')-&gt;user();
      }
  
      public function getContactList()
      {
  
          $client = new \GuzzleHttp\Client();
  
          $email = \Auth::user()-&gt;email;
  
          $token = \Session::get('token');
  
          $json = $client-&gt;get('https://www.google.com/m8/feeds/contacts/'. $email . '/full', [
              'query' =&gt; [
                  'prettyPrint' =&gt; 'false',
              ],
              'headers' =&gt; [
                  'Accept' =&gt; 'application/json',
                  'Authorization' =&gt; 'Bearer ' . $token ,
              ],
          ]);
  
          dd($json);
  
          return $json;
  
      }
  }
  </code></pre>
  
  <p>Here's my AuthController.php</p>
  
  <pre><code>&lt;?php namespace App\Http\Controllers;
  
  use App\AuthenticateUser;
  use App\AuthenticateUserListener;
  use App\Http\Requests;
  use App\Http\Controllers\Controller;
  use Laravel\Socialite\Two\GoogleProvider as Google;
  
  use Illuminate\Http\Request;
  
  class AuthController extends Controller implements AuthenticateUserListener
  {
  
      public function login(AuthenticateUser $authenticateUser, Request $request){
  
         return $authenticateUser-&gt;execute($request-&gt;has('code'), $this);
      }
  
      public function userHasLoggedIn($user)
      {
  
          return redirect('/');
      }
  
      public function logout(AuthenticateUser $authenticateUser){
  
  
          return $authenticateUser-&gt;logout();
  
      }
  
      public function getContactList(AuthenticateUser $authenticateUser)
      {
          $response = $authenticateUser-&gt;getContactList();
  
          dd($response);
      }
  }
  </code></pre>
  
  <p>Here's my MainController.php</p>
  
  <pre><code>&lt;?php namespace App\Http\Controllers;
  
  use App\Http\Requests;
  use App\Http\Controllers\Controller;
  
  use Illuminate\Http\Request;
  
  class MainController extends Controller {
  
  
      public function index()
      {
          if (\Auth::check()) return redirect('google_welcome');
          return redirect('google_login');
      }
  
      public function first()
      {
          return view('google_login');
      }
  
      public function back()
      {
          $user = \Auth::user();
          return view('google_welcomeback')-&gt;with('user', $user);
      }
  
  }
  </code></pre>
  
  <p>I'm fairly new to the PHP and Laravel universe, what's with trying to immediately use Google APIs and packages such as socialite which is using oAuth 2. I have really struggled to make the most out of my limited knowledge and haven't really found much documentation online, and the problem is my employer hinted that I either have to complete this as fastly as possible or he's gonna show me the way out...</p>