/** * Tests setting query params to the request. */ public function testHttpRequestQueryParams() { $request = new NostoHttpRequest(); $request->setQueryParams(array('param1' => 'first', 'param2' => 'second')); $params = $request->getQueryParams(); $this->assertArrayHasKey('param1', $params); $this->assertContains('first', $params); $this->assertArrayHasKey('param2', $params); $this->assertContains('second', $params); }
/** * @inheritdoc */ public static function syncFromNosto(NostoOAuthClientMetaDataInterface $meta, $code) { $oauthClient = new NostoOAuthClient($meta); $token = $oauthClient->authenticate($code); if (empty($token->accessToken)) { throw new NostoException('No access token found when trying to sync account from Nosto'); } if (empty($token->merchantName)) { throw new NostoException('No merchant name found when trying to sync account from Nosto'); } $request = new NostoHttpRequest(); // The request is currently not made according the the OAuth2 spec with the access token in the // Authorization header. This is due to the authentication server not implementing the full OAuth2 spec yet. $request->setUrl(NostoOAuthClient::$baseUrl . '/exchange'); $request->setQueryParams(array('access_token' => $token->accessToken)); $response = $request->get(); $result = $response->getJsonResult(true); if ($response->getCode() !== 200) { Nosto::throwHttpException('Failed to sync account from Nosto.', $request, $response); } if (empty($result)) { throw new NostoException('Received invalid data from Nosto when trying to sync account'); } $account = new self($token->merchantName); $account->tokens = NostoApiToken::parseTokens($result, 'api_'); if (!$account->isConnectedToNosto()) { throw new NostoException('Failed to sync all account details from Nosto'); } return $account; }
/** * Syncs an existing Nosto account via OAuth. * * Requires that the OAuth cycle has already completed the first step in getting the authorization code. * * @param NostoOauthClientMetaInterface $meta the OAuth client meta data to use for connection to Nosto. * @param string $authCode the authorization code that grants access to transfer data from Nosto. * @return NostoAccount the synced account. * * @throws NostoException on failure. */ public function sync(NostoOauthClientMetaInterface $meta, $authCode) { $oauthClient = new NostoOAuthClient($meta); $token = $oauthClient->authenticate($authCode); $request = new NostoHttpRequest(); // The request is currently not made according the the OAuth2 spec with the access token in the // Authorization header. This is due to the authentication server not implementing the full OAuth2 spec yet. $request->setUrl(NostoOAuthClient::$baseUrl . '/exchange'); $request->setQueryParams(array('access_token' => $token->getAccessToken())); $response = $request->get(); if ($response->getCode() !== 200) { throw Nosto::createHttpException('Failed to send account sync to Nosto.', $request, $response); } $result = $response->getJsonResult(true); $account = new NostoAccount($token->getMerchantName()); $tokens = NostoApiToken::parseTokens($result, 'api_'); foreach ($tokens as $token) { $account->addApiToken($token); } if (!$account->isConnectedToNosto()) { throw new NostoException('Failed to sync all account details from Nosto. Unknown error.'); } return $account; }