RunKeeper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /**
  11. * RunKeeper service.
  12. *
  13. * @link http://runkeeper.com/developer/healthgraph/registration-authorization
  14. */
  15. class RunKeeper extends AbstractService
  16. {
  17. public function __construct(
  18. CredentialsInterface $credentials,
  19. ClientInterface $httpClient,
  20. TokenStorageInterface $storage,
  21. $scopes = array(),
  22. UriInterface $baseApiUri = null
  23. ) {
  24. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  25. if (null === $baseApiUri) {
  26. $this->baseApiUri = new Uri('https://api.runkeeper.com/');
  27. }
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getAuthorizationUri(array $additionalParameters = array())
  33. {
  34. $parameters = array_merge(
  35. $additionalParameters,
  36. array(
  37. 'client_id' => $this->credentials->getConsumerId(),
  38. 'redirect_uri' => $this->credentials->getCallbackUrl(),
  39. 'response_type' => 'code',
  40. )
  41. );
  42. $parameters['scope'] = implode(' ', $this->scopes);
  43. // Build the url
  44. $url = clone $this->getAuthorizationEndpoint();
  45. foreach ($parameters as $key => $val) {
  46. $url->addToQuery($key, $val);
  47. }
  48. return $url;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getAuthorizationEndpoint()
  54. {
  55. return new Uri('https://runkeeper.com/apps/authorize');
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getAccessTokenEndpoint()
  61. {
  62. return new Uri('https://runkeeper.com/apps/token');
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function getAuthorizationMethod()
  68. {
  69. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. protected function parseAccessTokenResponse($responseBody)
  75. {
  76. $data = json_decode($responseBody, true);
  77. if (null === $data || !is_array($data)) {
  78. throw new TokenResponseException('Unable to parse response.');
  79. } elseif (isset($data['error'])) {
  80. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  81. }
  82. $token = new StdOAuth2Token();
  83. $token->setAccessToken($data['access_token']);
  84. unset($data['access_token']);
  85. $token->setExtraParams($data);
  86. return $token;
  87. }
  88. }