/**
  * @throws MissingConfigurationException
  * @return \Google_Client
  */
 public function create()
 {
     $client = new \Google_Client();
     $requiredAuthenticationSettings = array('applicationName', 'clientId', 'clientSecret', 'developerKey');
     foreach ($requiredAuthenticationSettings as $key) {
         if (!isset($this->authenticationSettings[$key])) {
             throw new MissingConfigurationException(sprintf('Missing setting "TYPO3.Neos.GoogleAnalytics.authentication.%s"', $key), 1415796352);
         }
     }
     $client->setApplicationName($this->authenticationSettings['applicationName']);
     $client->setClientId($this->authenticationSettings['clientId']);
     $client->setClientSecret($this->authenticationSettings['clientSecret']);
     $client->setDeveloperKey($this->authenticationSettings['developerKey']);
     $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
     $client->setAccessType('offline');
     $accessToken = $this->tokenStorage->getAccessToken();
     if ($accessToken !== NULL) {
         $client->setAccessToken($accessToken);
         if ($client->isAccessTokenExpired()) {
             $refreshToken = $this->tokenStorage->getRefreshToken();
             $client->refreshToken($refreshToken);
         }
     }
     return $client;
 }
 /**
  * @return void
  */
 public function authenticateAction()
 {
     $client = $this->analytics->getClient();
     $redirectUri = $this->uriBuilder->reset()->setCreateAbsoluteUri(TRUE)->uriFor('authenticate');
     $client->setRedirectUri($this->removeUriQueryArguments($redirectUri));
     // We have to get the "code" query argument without a module prefix
     $code = $this->request->getHttpRequest()->getArgument('code');
     if (!empty($code)) {
         $client->authenticate($code);
         $this->tokenStorage->storeAccessToken($client->getAccessToken());
         $this->tokenStorage->storeRefreshToken($client->getRefreshToken());
         $indexUri = $this->uriBuilder->reset()->setCreateAbsoluteUri(TRUE)->uriFor('index');
         $this->redirectToUri($this->removeUriQueryArguments($indexUri));
     }
     // If we don't have a refresh token, require an approval prompt to receive a refresh token
     $refreshToken = $this->tokenStorage->getRefreshToken();
     if ($refreshToken === NULL) {
         $client->setApprovalPrompt('force');
     }
     $authUrl = $client->createAuthUrl();
     $this->view->assign('authUrl', $authUrl);
 }