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