Vimeo.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Vimeo service.
  4. *
  5. * @author Pedro Amorim <contact@pamorim.fr>
  6. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  7. * @link https://developer.vimeo.com/
  8. * @link https://developer.vimeo.com/api/authentication
  9. */
  10. namespace OAuth\OAuth2\Service;
  11. use OAuth\OAuth2\Token\StdOAuth2Token;
  12. use OAuth\Common\Http\Exception\TokenResponseException;
  13. use OAuth\Common\Http\Uri\Uri;
  14. use OAuth\Common\Consumer\CredentialsInterface;
  15. use OAuth\Common\Http\Client\ClientInterface;
  16. use OAuth\Common\Storage\TokenStorageInterface;
  17. use OAuth\Common\Http\Uri\UriInterface;
  18. /**
  19. * Vimeo service.
  20. *
  21. * @author Pedro Amorim <contact@pamorim.fr>
  22. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  23. * @link https://developer.vimeo.com/
  24. * @link https://developer.vimeo.com/api/authentication
  25. */
  26. class Vimeo extends AbstractService
  27. {
  28. // API version
  29. const VERSION = '3.2';
  30. // API Header Accept
  31. const HEADER_ACCEPT = 'application/vnd.vimeo.*+json;version=3.2';
  32. /**
  33. * Scopes
  34. * @see https://developer.vimeo.com/api/authentication#scope
  35. */
  36. // View public videos
  37. const SCOPE_PUBLIC = 'public';
  38. // View private videos
  39. const SCOPE_PRIVATE = 'private';
  40. // View Vimeo On Demand purchase history
  41. const SCOPE_PURCHASED = 'purchased';
  42. // Create new videos, groups, albums, etc.
  43. const SCOPE_CREATE = 'create';
  44. // Edit videos, groups, albums, etc.
  45. const SCOPE_EDIT = 'edit';
  46. // Delete videos, groups, albums, etc.
  47. const SCOPE_DELETE = 'delete';
  48. // Interact with a video on behalf of a user, such as liking
  49. // a video or adding it to your watch later queue
  50. const SCOPE_INTERACT = 'interact';
  51. // Upload a video
  52. const SCOPE_UPLOAD = 'upload';
  53. public function __construct(
  54. CredentialsInterface $credentials,
  55. ClientInterface $httpClient,
  56. TokenStorageInterface $storage,
  57. $scopes = array(),
  58. UriInterface $baseApiUri = null
  59. ) {
  60. parent::__construct(
  61. $credentials,
  62. $httpClient,
  63. $storage,
  64. $scopes,
  65. $baseApiUri,
  66. true
  67. );
  68. if (null === $baseApiUri) {
  69. $this->baseApiUri = new Uri('https://api.vimeo.com/');
  70. }
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function getAuthorizationEndpoint()
  76. {
  77. return new Uri('https://api.vimeo.com/oauth/authorize');
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getAccessTokenEndpoint()
  83. {
  84. return new Uri('https://api.vimeo.com/oauth/access_token');
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. protected function getAuthorizationMethod()
  90. {
  91. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function parseAccessTokenResponse($responseBody)
  97. {
  98. $data = json_decode($responseBody, true);
  99. if (null === $data || !is_array($data)) {
  100. throw new TokenResponseException('Unable to parse response.');
  101. } elseif (isset($data['error_description'])) {
  102. throw new TokenResponseException(
  103. 'Error in retrieving token: "' . $data['error_description'] . '"'
  104. );
  105. } elseif (isset($data['error'])) {
  106. throw new TokenResponseException(
  107. 'Error in retrieving token: "' . $data['error'] . '"'
  108. );
  109. }
  110. $token = new StdOAuth2Token();
  111. $token->setAccessToken($data['access_token']);
  112. if (isset($data['expires_in'])) {
  113. $token->setLifeTime($data['expires_in']);
  114. unset($data['expires_in']);
  115. }
  116. if (isset($data['refresh_token'])) {
  117. $token->setRefreshToken($data['refresh_token']);
  118. unset($data['refresh_token']);
  119. }
  120. unset($data['access_token']);
  121. $token->setExtraParams($data);
  122. return $token;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. protected function getExtraOAuthHeaders()
  128. {
  129. return array('Accept' => self::HEADER_ACCEPT);
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. protected function getExtraApiHeaders()
  135. {
  136. return array('Accept' => self::HEADER_ACCEPT);
  137. }
  138. }