Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Harvest.php
001 <?php
002
003 namespace OAuth\OAuth2\Service;
004
005 use OAuth\Common\Consumer\CredentialsInterface;
006 use OAuth\Common\Http\Client\ClientInterface;
007 use OAuth\Common\Http\Exception\TokenResponseException;
008 use OAuth\Common\Http\Uri\Uri;
009 use OAuth\Common\Http\Uri\UriInterface;
010 use OAuth\Common\Storage\TokenStorageInterface;
011 use OAuth\Common\Token\TokenInterface;
012 use OAuth\OAuth2\Token\StdOAuth2Token;
013
014 class Harvest extends AbstractService
015 {
016
017 public function __construct(
018 CredentialsInterface $credentials,
019 ClientInterface $httpClient,
020 TokenStorageInterface $storage,
021 $scopes = array(),
022 UriInterface $baseApiUri = null
023 ) {
024 parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
025
026 if (null === $baseApiUri) {
027 $this->baseApiUri = new Uri('https://api.harvestapp.com/');
028 }
029 }
030
031 /**
032 * {@inheritdoc}
033 */
034 public function getAuthorizationUri(array $additionalParameters = array())
035 {
036 $parameters = array_merge(
037 $additionalParameters,
038 array(
039 'client_id' => $this->credentials->getConsumerId(),
040 'redirect_uri' => $this->credentials->getCallbackUrl(),
041 'state' => 'optional-csrf-token',
042 'response_type' => 'code',
043 )
044 );
045
046 // Build the url
047 $url = clone $this->getAuthorizationEndpoint();
048 foreach ($parameters as $key => $val) {
049 $url->addToQuery($key, $val);
050 }
051
052 return $url;
053 }
054
055 /**
056 * {@inheritdoc}
057 */
058 public function getAuthorizationEndpoint()
059 {
060 return new Uri('https://api.harvestapp.com/oauth2/authorize');
061 }
062
063 /**
064 * {@inheritdoc}
065 */
066 public function getAccessTokenEndpoint()
067 {
068 return new Uri('https://api.harvestapp.com/oauth2/token');
069 }
070
071 /**
072 * {@inheritdoc}
073 */
074 protected function getAuthorizationMethod()
075 {
076 return static::AUTHORIZATION_METHOD_QUERY_STRING;
077 }
078
079 /**
080 * {@inheritdoc}
081 */
082 protected function parseAccessTokenResponse($responseBody)
083 {
084 $data = json_decode($responseBody, true);
085
086 if (null === $data || ! is_array($data)) {
087 throw new TokenResponseException('Unable to parse response.');
088 } elseif (isset($data['error'])) {
089 throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
090 }
091
092 $token = new StdOAuth2Token();
093 $token->setAccessToken($data['access_token']);
094 $token->setLifetime($data['expires_in']);
095 $token->setRefreshToken($data['refresh_token']);
096
097 unset($data['access_token']);
098
099 $token->setExtraParams($data);
100
101 return $token;
102 }
103
104 /**
105 * Refreshes an OAuth2 access token.
106 *
107 * @param TokenInterface $token
108 *
109 * @return TokenInterface $token
110 *
111 * @throws MissingRefreshTokenException
112 */
113 public function refreshAccessToken(TokenInterface $token)
114 {
115 $refreshToken = $token->getRefreshToken();
116
117 if (empty($refreshToken)) {
118 throw new MissingRefreshTokenException();
119 }
120
121 $parameters = array(
122 'grant_type' => 'refresh_token',
123 'type' => 'web_server',
124 'client_id' => $this->credentials->getConsumerId(),
125 'client_secret' => $this->credentials->getConsumerSecret(),
126 'refresh_token' => $refreshToken,
127 );
128
129 $responseBody = $this->httpClient->retrieveResponse(
130 $this->getAccessTokenEndpoint(),
131 $parameters,
132 $this->getExtraOAuthHeaders()
133 );
134 $token = $this->parseAccessTokenResponse($responseBody);
135 $this->storage->storeAccessToken($this->service(), $token);
136
137 return $token;
138 }
139
140 /**
141 * @return array
142 */
143 protected function getExtraOAuthHeaders()
144 {
145 return array('Accept' => 'application/json');
146 }
147
148 /**
149 * Return any additional headers always needed for this service implementation's API calls.
150 *
151 * @return array
152 */
153 protected function getExtraApiHeaders()
154 {
155 return array('Accept' => 'application/json');
156 }
157 }
158