SoundCloud.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 SoundCloud extends AbstractService
  11. {
  12. public function __construct(
  13. CredentialsInterface $credentials,
  14. ClientInterface $httpClient,
  15. TokenStorageInterface $storage,
  16. $scopes = array(),
  17. UriInterface $baseApiUri = null
  18. ) {
  19. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  20. if (null === $baseApiUri) {
  21. $this->baseApiUri = new Uri('https://api.soundcloud.com/');
  22. }
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getAuthorizationEndpoint()
  28. {
  29. return new Uri('https://soundcloud.com/connect');
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getAccessTokenEndpoint()
  35. {
  36. return new Uri('https://api.soundcloud.com/oauth2/token');
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function parseAccessTokenResponse($responseBody)
  42. {
  43. $data = json_decode($responseBody, true);
  44. if (null === $data || !is_array($data)) {
  45. throw new TokenResponseException('Unable to parse response.');
  46. } elseif (isset($data['error'])) {
  47. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  48. }
  49. $token = new StdOAuth2Token();
  50. $token->setAccessToken($data['access_token']);
  51. if (isset($data['expires_in'])) {
  52. $token->setLifetime($data['expires_in']);
  53. unset($data['expires_in']);
  54. }
  55. if (isset($data['refresh_token'])) {
  56. $token->setRefreshToken($data['refresh_token']);
  57. unset($data['refresh_token']);
  58. }
  59. unset($data['access_token']);
  60. $token->setExtraParams($data);
  61. return $token;
  62. }
  63. }