Dropbox.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * Dropbox service.
  12. *
  13. * @author Flávio Heleno <flaviohbatista@gmail.com>
  14. * @link https://www.dropbox.com/developers/core/docs
  15. */
  16. class Dropbox extends AbstractService
  17. {
  18. public function __construct(
  19. CredentialsInterface $credentials,
  20. ClientInterface $httpClient,
  21. TokenStorageInterface $storage,
  22. $scopes = array(),
  23. UriInterface $baseApiUri = null
  24. ) {
  25. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  26. if (null === $baseApiUri) {
  27. $this->baseApiUri = new Uri('https://api.dropbox.com/1/');
  28. }
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getAuthorizationUri(array $additionalParameters = array())
  34. {
  35. $parameters = array_merge(
  36. $additionalParameters,
  37. array(
  38. 'client_id' => $this->credentials->getConsumerId(),
  39. 'redirect_uri' => $this->credentials->getCallbackUrl(),
  40. 'response_type' => 'code',
  41. )
  42. );
  43. $parameters['scope'] = implode(' ', $this->scopes);
  44. // Build the url
  45. $url = clone $this->getAuthorizationEndpoint();
  46. foreach ($parameters as $key => $val) {
  47. $url->addToQuery($key, $val);
  48. }
  49. return $url;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getAuthorizationEndpoint()
  55. {
  56. return new Uri('https://www.dropbox.com/1/oauth2/authorize');
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getAccessTokenEndpoint()
  62. {
  63. return new Uri('https://api.dropbox.com/1/oauth2/token');
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function getAuthorizationMethod()
  69. {
  70. return static::AUTHORIZATION_METHOD_QUERY_STRING;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function parseAccessTokenResponse($responseBody)
  76. {
  77. $data = json_decode($responseBody, true);
  78. if (null === $data || !is_array($data)) {
  79. throw new TokenResponseException('Unable to parse response.');
  80. } elseif (isset($data['error'])) {
  81. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  82. }
  83. $token = new StdOAuth2Token();
  84. $token->setAccessToken($data['access_token']);
  85. if (isset($data['refresh_token'])) {
  86. $token->setRefreshToken($data['refresh_token']);
  87. unset($data['refresh_token']);
  88. }
  89. unset($data['access_token']);
  90. $token->setExtraParams($data);
  91. return $token;
  92. }
  93. }