Ustream.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Ustream extends AbstractService
  11. {
  12. /**
  13. * Scopes
  14. *
  15. * @var string
  16. */
  17. const SCOPE_OFFLINE = 'offline';
  18. const SCOPE_BROADCASTER = 'broadcaster';
  19. public function __construct(
  20. CredentialsInterface $credentials,
  21. ClientInterface $httpClient,
  22. TokenStorageInterface $storage,
  23. $scopes = array(),
  24. UriInterface $baseApiUri = null
  25. ) {
  26. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
  27. if (null === $baseApiUri) {
  28. $this->baseApiUri = new Uri('https://api.ustream.tv/');
  29. }
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getAuthorizationEndpoint()
  35. {
  36. return new Uri('https://www.ustream.tv/oauth2/authorize');
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getAccessTokenEndpoint()
  42. {
  43. return new Uri('https://www.ustream.tv/oauth2/token');
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function getAuthorizationMethod()
  49. {
  50. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function parseAccessTokenResponse($responseBody)
  56. {
  57. $data = json_decode($responseBody, true);
  58. if (null === $data || !is_array($data)) {
  59. throw new TokenResponseException('Unable to parse response.');
  60. } elseif (isset($data['error'])) {
  61. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  62. }
  63. $token = new StdOAuth2Token();
  64. $token->setAccessToken($data['access_token']);
  65. $token->setLifeTime($data['expires_in']);
  66. if (isset($data['refresh_token'])) {
  67. $token->setRefreshToken($data['refresh_token']);
  68. unset($data['refresh_token']);
  69. }
  70. unset($data['access_token']);
  71. unset($data['expires_in']);
  72. $token->setExtraParams($data);
  73. return $token;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function getExtraOAuthHeaders()
  79. {
  80. return array('Authorization' => 'Basic ' . $this->credentials->getConsumerSecret());
  81. }
  82. }