public function refreshTokenCallback($accessToken, $refreshToken)
 {
     $account = $this->configuration->getAccountBy('accountId', $this->currAccountId);
     $account->setAccessToken($accessToken);
     $account->setRefreshToken($refreshToken);
     $account->save();
 }
 public function testPostSheets()
 {
     $this->createConfig();
     $this->createAccount();
     self::$client->request('POST', $this->componentName . '/sheets/' . $this->accountId, array(), array(), array(), json_encode(array('data' => array(array('googleId' => $this->fileGoogleId, 'title' => $this->fileTitle, 'sheetId' => $this->sheetId, 'sheetTitle' => $this->sheetTitle)))));
     $this->assertEquals(200, self::$client->getResponse()->getStatusCode());
     $account = $this->configuration->getAccountBy('accountId', $this->accountId);
     $sheets = $account->getSheets();
     $this->assertNotEmpty($sheets);
 }
 public function save($data, Sheet $sheet)
 {
     $sheetConfig = $sheet->getConfig();
     $tmpFilename = $this->writeRawCsv($data, $sheet);
     $dataProcessor = new DataProcessor($tmpFilename, $sheetConfig);
     $outFilename = $dataProcessor->process();
     $this->configuration->initDataBucket($sheetConfig['db']['table']);
     $outputTable = $sheetConfig['db']['table'];
     $tableNameArr = explode('.', $outputTable);
     if (count($tableNameArr) != 3) {
         throw new UserException(sprintf("Error in configuration. Wrong tableId format '%s'", $outputTable));
     }
     $table = new Table($this->configuration->getStorageApi(), $outputTable, $outFilename);
     try {
         $table->save(true);
     } catch (ClientException $e) {
         throw new UserException($e->getMessage(), $e, ['outputTable' => $outputTable, 'sheet' => $sheet->toArray()]);
     }
     unlink($tmpFilename);
 }
 /**
  * @param Request $request
  * @return JsonResponse
  */
 public function postConfigsAction(Request $request)
 {
     $params = $this->getPostJson($request);
     $this->checkParams(['name'], $params);
     try {
         $this->getConfiguration()->exists();
     } catch (ConfigurationException $e) {
         $this->getConfiguration()->create();
     }
     if (null != $this->getConfiguration()->getAccountBy('accountId', $this->configuration->getIdFromName($params['name']))) {
         throw new ConfigurationException('Account already exists');
     }
     $params['accountName'] = $params['name'];
     $account = $this->getConfiguration()->addAccount($params);
     return $this->getJsonResponse(['id' => $account->getAccountId(), 'name' => $account->getAccountName(), 'description' => $account->getDescription()]);
 }
Example #5
0
 protected function initConfiguration()
 {
     $this->configuration->setStorageApi($this->storageApi);
     return $this->configuration;
 }
Example #6
0
 public function getInBucketId()
 {
     return $this->configuration->getInBucketId($this->accountId);
 }
 public function oauthCallbackAction()
 {
     $session = $this->get('session');
     $token = $session->get('token');
     $accountId = $session->get('account');
     $referrer = $session->get('referrer');
     if ($token == null) {
         throw new UserException("Your session expired, please try again");
     }
     $code = $this->get('request')->query->get('code');
     if (empty($code)) {
         throw new SyrupComponentException(400, 'Could not read from Google API');
     }
     $googleApi = $this->getGoogleApi();
     try {
         $storageApi = new StorageApi(['token' => $token, 'url' => null, 'userAgent' => $this->componentName]);
         $tokenData = $storageApi->verifyToken();
         /** @var EncryptorInterface $encryptor */
         $encryptor = $this->get('syrup.encryptor');
         $configuration = new Configuration($this->componentName, $encryptor);
         $configuration->setStorageApi($storageApi);
         $tokens = $googleApi->authorize($code, $this->container->get('router')->generate('keboola_google_drive_oauth_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL));
         $googleApi->setCredentials($tokens['access_token'], $tokens['refresh_token']);
         $userData = json_decode($googleApi->request('https://www.googleapis.com/oauth2/v2/userinfo')->getBody(), true);
         $account = $configuration->getAccountBy('accountId', $accountId);
         if (null == $account) {
             throw new ConfigurationException("Account doesn't exist");
         }
         $account->setGoogleId($userData['id'])->setGoogleName($userData['name'])->setEmail($userData['email'])->setAccessToken($tokens['access_token'])->setRefreshToken($tokens['refresh_token'])->setOwner($tokenData['description']);
         if ($account->isExternal()) {
             if ($userData['email'] == $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();
         $this->container->get('session')->clear();
         if ($referrer) {
             return new RedirectResponse($referrer);
         } else {
             return new JsonResponse(array('status' => 'ok'));
         }
     } catch (\Exception $e) {
         throw new SyrupComponentException(500, 'Could not save API tokens', $e);
     }
 }