Netatmo.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Netatmo service.
  4. *
  5. * @author Pedro Amorim <contact@pamorim.fr>
  6. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  7. * @link https://dev.netatmo.com/doc/
  8. */
  9. namespace OAuth\OAuth2\Service;
  10. use OAuth\OAuth2\Token\StdOAuth2Token;
  11. use OAuth\Common\Http\Exception\TokenResponseException;
  12. use OAuth\Common\Http\Uri\Uri;
  13. use OAuth\Common\Consumer\CredentialsInterface;
  14. use OAuth\Common\Http\Client\ClientInterface;
  15. use OAuth\Common\Storage\TokenStorageInterface;
  16. use OAuth\Common\Http\Uri\UriInterface;
  17. /**
  18. * Netatmo service.
  19. *
  20. * @author Pedro Amorim <contact@pamorim.fr>
  21. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  22. * @link https://dev.netatmo.com/doc/
  23. */
  24. class Netatmo extends AbstractService
  25. {
  26. // SCOPES
  27. // @link https://dev.netatmo.com/doc/authentication/scopes
  28. // Used to read weather station's data (devicelist, getmeasure)
  29. const SCOPE_STATION_READ = 'read_station';
  30. // Used to read thermostat's data (devicelist, getmeasure, getthermstate)
  31. const SCOPE_THERMOSTAT_READ = 'read_thermostat';
  32. // Used to configure the thermostat (syncschedule, setthermpoint)
  33. const SCOPE_THERMOSTAT_WRITE = 'write_thermostat';
  34. public function __construct(
  35. CredentialsInterface $credentials,
  36. ClientInterface $httpClient,
  37. TokenStorageInterface $storage,
  38. $scopes = array(),
  39. UriInterface $baseApiUri = null
  40. ) {
  41. parent::__construct(
  42. $credentials,
  43. $httpClient,
  44. $storage,
  45. $scopes,
  46. $baseApiUri,
  47. true // use parameter state
  48. );
  49. if (null === $baseApiUri) {
  50. $this->baseApiUri = new Uri('https://api.netatmo.net/');
  51. }
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getAuthorizationEndpoint()
  57. {
  58. return new Uri($this->baseApiUri.'oauth2/authorize');
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getAccessTokenEndpoint()
  64. {
  65. return new Uri($this->baseApiUri.'oauth2/token');
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function getAuthorizationMethod()
  71. {
  72. return static::AUTHORIZATION_METHOD_QUERY_STRING;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function parseAccessTokenResponse($responseBody)
  78. {
  79. $data = json_decode($responseBody, true);
  80. if (null === $data || !is_array($data)) {
  81. throw new TokenResponseException('Unable to parse response.');
  82. } elseif (isset($data['error'])) {
  83. throw new TokenResponseException(
  84. 'Error in retrieving token: "' . $data['error'] . '"'
  85. );
  86. }
  87. $token = new StdOAuth2Token();
  88. $token->setAccessToken($data['access_token']);
  89. $token->setLifetime($data['expires_in']);
  90. if (isset($data['refresh_token'])) {
  91. $token->setRefreshToken($data['refresh_token']);
  92. unset($data['refresh_token']);
  93. }
  94. unset($data['access_token']);
  95. unset($data['expires_in']);
  96. $token->setExtraParams($data);
  97. return $token;
  98. }
  99. }