/**
  * /login
  * Authenticate the user via Google OAuth then redirect to homepage.
  *
  * @param  Request       $request
  * @param  Google_Client $googleClient
  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
  */
 public function anyIndex(Request $request, Google_Client $googleClient)
 {
     if ($code = $request->input('code')) {
         if ($request->session()->get('state') != $request->input('state')) {
             App::abort(400, 'The session state did not match.');
         }
         $googleClient->authenticate($code);
         // Now get the user and log them in to the app
         $oauth2 = new Google_Service_Oauth2($googleClient);
         $userInfo = $oauth2->userinfo->get();
         if ($user = User::where('google_user_id', $userInfo->id)->first()) {
             // User is already in DB.
         } else {
             // Create a new user.
             $user = new User(['google_user_id' => $userInfo->id]);
         }
         $user->setAccessToken($googleClient->getAccessToken(), $googleClient->getRefreshToken());
         $user->name = $userInfo->name;
         $user->save();
         Auth::login($user);
         return redirect()->secure('/');
     }
     $state = (string) mt_rand();
     $googleClient->setState($state);
     $request->session()->flash('state', $state);
     $googleClient->setApprovalPrompt('force');
     $authUrl = $googleClient->createAuthUrl();
     return redirect($authUrl);
 }
 /**
  * Change the video's description via the YouTube API.
  *
  * @return boolean
  */
 public function execute()
 {
     $googleClient = App::make('Google_Client');
     $user = User::find($this->user_id);
     $googleClient->setAccessToken($user->access_token);
     $youtube = new Google_Service_YouTube($googleClient);
     $listResponse = $youtube->videos->listVideos("snippet", array('id' => $this->video_id));
     if (count($listResponse) < 1) {
         return false;
     }
     $video = $listResponse[0];
     $videoSnippet = $video['snippet'];
     $videoSnippet['description'] = $this->description;
     $updateResponse = $youtube->videos->update("snippet", $video);
     $this->success = $updateResponse->snippet->description === $this->description;
     $this->executed_at = (new DateTime())->format('Y-m-d H:i:s');
     $this->save();
     return $this->success;
 }
 public function refreshAccessTokens()
 {
     $googleClient = App::make('Google_Client');
     // Get all users
     $users = User::all();
     foreach ($users as $user) {
         if ($user->refresh_token) {
             $this->log->info("Access Token for {$user->name} expires at {$user->access_token_expires}");
             // Pre-emptively renew the token if it expires in 15 mins
             $expires = strtotime($user->access_token_expires);
             $cutoff = strtotime("+15 MINUTES");
             // Or if Google tells us it's already expires
             $googleClient->setAccessToken($user->access_token);
             if ($expires < $cutoff || $googleClient->isAccessTokenExpired()) {
                 $this->log->info("Refreshing token for '{$user->name}'.");
                 $user->refreshAccessToken();
                 $this->log->info("Token for '{$user->name}' now expires at {$user->access_token_expires}");
                 $user->save();
             }
         } else {
             $this->log->error("User '{$user->name}' has no refresh token.");
         }
     }
 }