Dailymotion.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * Dailymotion service.
  12. *
  13. * @author Mouhamed SEYE <mouhamed@seye.pro>
  14. * @link http://www.dailymotion.com/doc/api/authentication.html
  15. */
  16. class Dailymotion extends AbstractService
  17. {
  18. /**
  19. * Scopes
  20. *
  21. * @var string
  22. */
  23. const SCOPE_EMAIL = 'email',
  24. SCOPE_PROFILE = 'userinfo',
  25. SCOPE_VIDEOS = 'manage_videos',
  26. SCOPE_COMMENTS = 'manage_comments',
  27. SCOPE_PLAYLIST = 'manage_playlists',
  28. SCOPE_TILES = 'manage_tiles',
  29. SCOPE_SUBSCRIPTIONS = 'manage_subscriptions',
  30. SCOPE_FRIENDS = 'manage_friends',
  31. SCOPE_FAVORITES = 'manage_favorites',
  32. SCOPE_GROUPS = 'manage_groups';
  33. /**
  34. * Dialog form factors
  35. *
  36. * @var string
  37. */
  38. const DISPLAY_PAGE = 'page',
  39. DISPLAY_POPUP = 'popup',
  40. DISPLAY_MOBILE = 'mobile';
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function __construct(
  45. CredentialsInterface $credentials,
  46. ClientInterface $httpClient,
  47. TokenStorageInterface $storage,
  48. $scopes = array(),
  49. UriInterface $baseApiUri = null
  50. ) {
  51. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  52. if (null === $baseApiUri) {
  53. $this->baseApiUri = new Uri('https://api.dailymotion.com/');
  54. }
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getAuthorizationEndpoint()
  60. {
  61. return new Uri('https://api.dailymotion.com/oauth/authorize');
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getAccessTokenEndpoint()
  67. {
  68. return new Uri('https://api.dailymotion.com/oauth/token');
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. protected function getAuthorizationMethod()
  74. {
  75. return static::AUTHORIZATION_METHOD_HEADER_OAUTH;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function parseAccessTokenResponse($responseBody)
  81. {
  82. $data = json_decode($responseBody, true);
  83. if (null === $data || !is_array($data)) {
  84. throw new TokenResponseException('Unable to parse response.');
  85. } elseif (isset($data['error_description']) || isset($data['error'])) {
  86. throw new TokenResponseException(
  87. sprintf(
  88. 'Error in retrieving token: "%s"',
  89. isset($data['error_description']) ? $data['error_description'] : $data['error']
  90. )
  91. );
  92. }
  93. $token = new StdOAuth2Token();
  94. $token->setAccessToken($data['access_token']);
  95. $token->setLifeTime($data['expires_in']);
  96. if (isset($data['refresh_token'])) {
  97. $token->setRefreshToken($data['refresh_token']);
  98. unset($data['refresh_token']);
  99. }
  100. unset($data['access_token']);
  101. unset($data['expires_in']);
  102. $token->setExtraParams($data);
  103. return $token;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. protected function getExtraOAuthHeaders()
  109. {
  110. return array('Accept' => 'application/json');
  111. }
  112. }