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 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); } }