public function refreshTokenCallback($accessToken, $refreshToken)
 {
     $account = $this->configuration->getAccountBy('accountId', $this->currAccountId);
     $account->setAccessToken($accessToken);
     $account->setRefreshToken($refreshToken);
     $account->save();
 }
 /**
  * Profiles
  */
 public function testPostProfiles()
 {
     $this->createConfig();
     $this->createAccount();
     self::$client->request('POST', '/ex-google-analytics/profiles/test', array(), array(), array(), json_encode(array(array('googleId' => '987654321', 'accountId' => '567890', 'accountName' => 'accountTest', 'name' => 'testProfile', 'webPropertyId' => 'web-property-id', 'webPropertyName' => 'web-property-name'))));
     /* @var Response $responseJson */
     $responseJson = self::$client->getResponse()->getContent();
     $response = json_decode($responseJson, true);
     $this->assertEquals("ok", $response['status']);
     $account = $this->configuration->getAccountBy('accountId', 'test');
     $profiles = $account->getProfiles();
     $this->assertNotEmpty($profiles);
     $this->assertCount(1, $profiles);
     /** @var Profile $profile */
     $profile = $profiles[0];
     $this->assertEquals('987654321', $profile->getGoogleId());
     $this->assertEquals('testProfile', $profile->getName());
 }
 public function oauthCallbackAction()
 {
     /** @var Session $session */
     $session = $this->get('session');
     $googleApi = $this->getGoogleApi();
     $token = $session->get('token');
     $accountId = $session->get('account');
     $referrer = $session->get('referrer');
     $session->clear();
     $code = $this->get('request')->query->get('code');
     if (empty($token) || empty($accountId)) {
         throw new UserException('Auth session expired');
     }
     if (empty($code)) {
         throw new UserException('Could not read from Google API');
     }
     try {
         $storageApi = new StorageApi(['token' => $token, 'userAgent' => 'ex-google-analytics']);
         $tokenData = $storageApi->verifyToken();
         /** @var EncryptorInterface $encryptor */
         $encryptor = $this->get('syrup.encryptor');
         $configuration = new Configuration('ex-google-analytics', $encryptor);
         $configuration->setStorageApi($storageApi);
         $tokens = $googleApi->authorize($code, $this->container->get('router')->generate('keboola_google_analytics_oauth_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL));
         $googleApi->setCredentials($tokens['access_token'], $tokens['refresh_token']);
         $userData = json_decode($googleApi->request('/oauth2/v2/userinfo')->getBody(), true);
         /** @var Account $account */
         $account = $configuration->getAccountBy('accountId', $accountId);
         if (null == $account) {
             throw new ConfigurationException("Account doesn't exist");
         }
         $userName = isset($userData['name']) ? $userData['name'] : $userData['displayName'];
         $userEmail = isset($userData['email']) ? $userData['email'] : $userData['emails'][0]['value'];
         $account->setGoogleId($userData['id'])->setGoogleName($userName)->setEmail($userEmail)->setAccessToken($tokens['access_token'])->setRefreshToken($tokens['refresh_token'])->setOwner($tokenData['description']);
         if ($account->isExternal()) {
             if ($userEmail == $tokenData['description'] || !isset($tokenData['creatorToken'])) {
                 // user generated an external link for himself or is reauthorizing himself into his config which was external before
                 $account->setExternal(false);
             } else {
                 $account->setOwner($tokenData['creatorToken']['description']);
             }
         }
         $account->save();
         if ($referrer) {
             return new RedirectResponse($referrer);
         } else {
             return new JsonResponse(array('status' => 'ok'));
         }
     } catch (ClientException $e) {
         // SAPI exception - probably invalid access token
         throw new UserException($e->getMessage());
     } catch (\Exception $e) {
         // any other exception
         throw new ApplicationException('Error finishing authorization: ' . $e->getMessage(), $e);
     }
 }