Foursquare.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\OAuth2\Token\StdOAuth2Token;
  4. use OAuth\Common\Http\Exception\TokenResponseException;
  5. use OAuth\Common\Http\Uri\Uri;
  6. use OAuth\Common\Consumer\CredentialsInterface;
  7. use OAuth\Common\Http\Client\ClientInterface;
  8. use OAuth\Common\Storage\TokenStorageInterface;
  9. use OAuth\Common\Http\Uri\UriInterface;
  10. class Foursquare extends AbstractService
  11. {
  12. private $apiVersionDate = '20130829';
  13. public function __construct(
  14. CredentialsInterface $credentials,
  15. ClientInterface $httpClient,
  16. TokenStorageInterface $storage,
  17. $scopes = array(),
  18. UriInterface $baseApiUri = null
  19. ) {
  20. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  21. if (null === $baseApiUri) {
  22. $this->baseApiUri = new Uri('https://api.foursquare.com/v2/');
  23. }
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getAuthorizationEndpoint()
  29. {
  30. return new Uri('https://foursquare.com/oauth2/authenticate');
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getAccessTokenEndpoint()
  36. {
  37. return new Uri('https://foursquare.com/oauth2/access_token');
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function parseAccessTokenResponse($responseBody)
  43. {
  44. $data = json_decode($responseBody, true);
  45. if (null === $data || !is_array($data)) {
  46. throw new TokenResponseException('Unable to parse response.');
  47. } elseif (isset($data['error'])) {
  48. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  49. }
  50. $token = new StdOAuth2Token();
  51. $token->setAccessToken($data['access_token']);
  52. // Foursquare tokens evidently never expire...
  53. $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
  54. unset($data['access_token']);
  55. $token->setExtraParams($data);
  56. return $token;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function request($path, $method = 'GET', $body = null, array $extraHeaders = array())
  62. {
  63. $uri = $this->determineRequestUriFromPath($path, $this->baseApiUri);
  64. $uri->addToQuery('v', $this->apiVersionDate);
  65. return parent::request($uri, $method, $body, $extraHeaders);
  66. }
  67. }