/** * @param mixed $token * @param string $resourceUrl * @param string $requestBody * @param array $extraParameters * @param string $httpMethod * @param string $responseType * @return string */ public function fetchRequest($token, $resourceUrl, $requestBody = null, $extraParameters = [], $httpMethod = HttpRequestInterface::METHOD_GET, $responseType = self::INTUIT_RESPONSE_JSON) { /** @var OAuthToken $refreshToken */ $accessToken = $token instanceof OAuthToken ? $token->getRawToken() : (array) $token; $parameters = array_merge(['oauth_consumer_key' => $this->options['client_id'], 'oauth_timestamp' => time(), 'oauth_nonce' => $this->generateNonce(), 'oauth_version' => '1.0', 'oauth_signature_method' => $this->options['signature_method'], 'oauth_token' => $accessToken['oauth_token']], $extraParameters); $parameters['oauth_signature'] = OAuthUtils::signRequest($httpMethod, $resourceUrl, $parameters, $this->options['client_secret'], $accessToken['oauth_token_secret'], $this->options['signature_method']); $headers = $this->createRequestHeader($resourceUrl, $responseType); $response = $this->httpRequest($resourceUrl, $requestBody, $parameters, $headers, $httpMethod); $content = $response->getContent(); return $content; }
/** * {@inheritDoc} */ public function getUserInformation(array $accessToken, array $extraParameters = array()) { $parameters = array_merge(array('oauth_consumer_key' => $this->options['client_id'], 'oauth_timestamp' => time(), 'oauth_nonce' => $this->generateNonce(), 'oauth_version' => '1.0', 'oauth_signature_method' => $this->options['signature_method'], 'oauth_token' => $accessToken['oauth_token']), $extraParameters); $parameters['oauth_signature'] = OAuthUtils::signRequest(HttpRequestInterface::METHOD_GET, $this->options['infos_session_url'], $parameters, $this->options['client_secret'], $accessToken['oauth_token_secret'], $this->options['signature_method']); $content = $this->getResponseContent($this->doGetUserInformationRequest($this->options['infos_session_url'], $parameters)); $url = $this->normalizeUrl($this->options['infos_url'], array('username' => $content['name'])); // Regenerate nonce & signature as URL was changed $parameters['oauth_nonce'] = $this->generateNonce(); $parameters['oauth_signature'] = OAuthUtils::signRequest(HttpRequestInterface::METHOD_GET, $url, $parameters, $this->options['client_secret'], $accessToken['oauth_token_secret'], $this->options['signature_method']); $content = $this->doGetUserInformationRequest($url, $parameters)->getContent(); $response = $this->getUserResponse(); $response->setResponse($content); $response->setResourceOwner($this); $response->setOAuthToken(new OAuthToken($accessToken)); return $response; }
/** * @return boolean|Bundle */ public function tweet() { /* @var $trendingBundle Bundle*/ if (!($trendingBundle = $this->em->getRepository('KnpBundlesBundle:Bundle')->findLatestTrend($this->twitterParams['idle_period']))) { throw new TrendingBundleNotFoundException(); } $timestamp = time(); $parameters = array('oauth_consumer_key' => $this->twitterParams['oauth_client_id'], 'oauth_timestamp' => $timestamp, 'oauth_nonce' => md5(microtime() . mt_rand()), 'oauth_version' => '1.0', 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_token' => $this->twitterParams['oauth_token']); $parameters['oauth_signature'] = OAuthUtils::signRequest('POST', $this->twitterApiUrl, $parameters, $this->twitterParams['oauth_client_secret'], $this->twitterParams['oauth_token_secret']); $response = $this->httpRequest($this->twitterApiUrl, $this->prepareMessage($trendingBundle), $parameters, array(), 'POST'); if ($response->isSuccessful()) { $trendingBundle->setLastTweetedAt(new \DateTime()); $this->em->persist($trendingBundle); $activity = new Activity(); $activity->setType(Activity::ACTIVITY_TYPE_TWEETED); $activity->setCreatedAt(new \DateTime()); $activity->setBundle($trendingBundle); $this->em->persist($activity); $this->em->flush(); return $trendingBundle; } return false; }
/** * {@inheritDoc} */ public function getRequestToken($redirectUri, array $extraParameters = array()) { $timestamp = time(); $parameters = array_merge(array('oauth_consumer_key' => $this->options['client_id'], 'oauth_timestamp' => $timestamp, 'oauth_nonce' => $this->generateNonce(), 'oauth_version' => '1.0', 'oauth_callback' => $redirectUri, 'oauth_signature_method' => $this->options['signature_method']), $extraParameters); $url = $this->options['request_token_url']; $parameters['oauth_signature'] = OAuthUtils::signRequest(HttpRequestInterface::METHOD_POST, $url, $parameters, $this->options['client_secret'], '', $this->options['signature_method']); $apiResponse = $this->httpRequest($url, null, $parameters, array(), HttpRequestInterface::METHOD_POST); $response = $this->getResponseContent($apiResponse); if (isset($response['oauth_problem'])) { throw new AuthenticationException(sprintf('OAuth error: "%s"', $response['oauth_problem'])); } if (isset($response['oauth_callback_confirmed']) && $response['oauth_callback_confirmed'] != 'true') { throw new AuthenticationException('Defined OAuth callback was not confirmed.'); } if (!isset($response['oauth_token']) || !isset($response['oauth_token_secret'])) { throw new AuthenticationException('Not a valid request token.'); } $response['timestamp'] = $timestamp; $this->storage->save($this, $response); return $response; }
/** * @dataProvider provideInvalidData * @expectedException \RuntimeException */ public function testThrowsExceptionIfRequiredParameterIsMissing($parameters) { OAuthUtils::signRequest('GET', 'http://example.com', $parameters, 'client_secret'); }
/** * {@inheritDoc} */ protected function getRequestToken($redirectUri, array $extraParameters = array()) { $timestamp = time(); $parameters = array_merge($extraParameters, array('oauth_consumer_key' => $this->getOption('client_id'), 'oauth_timestamp' => $timestamp, 'oauth_nonce' => $this->generateNonce(), 'oauth_version' => '1.0', 'oauth_callback' => $redirectUri, 'oauth_signature_method' => 'HMAC-SHA1')); $url = $this->getOption('request_token_url'); $parameters['oauth_signature'] = OAuthUtils::signRequest('POST', $url, $parameters, $this->getOption('client_secret')); $apiResponse = $this->httpRequest($url, null, $parameters, array(), 'POST'); $response = $this->getResponseContent($apiResponse); if (isset($response['oauth_problem']) || isset($response['oauth_callback_confirmed']) && $response['oauth_callback_confirmed'] != 'true') { throw new AuthenticationException(sprintf('OAuth error: "%s"', $response['oauth_problem'])); } if (!isset($response['oauth_token']) || !isset($response['oauth_token_secret'])) { throw new AuthenticationException('Not a valid request token.'); } $response['timestamp'] = $timestamp; $this->storage->save($this, $response); return $response; }