Skip to content

Commit ce8b2f9

Browse files
Add ability to override access token request headers (#603)
* Add ability to override access token request headers This moves the currently hardcoded `getAccessTokenResponse` into their own method. This allows children providers to construct their own headers as needed. A perfect example of the necessity of this feature is the newer Pinterest v5 API. In order to exchange the code for an access token, they require an `Authorization` header composed of a base64 encoded `client_id` and `client_secret`. Without this change, the Pinterest Provider would need to entirely reimplement the `getAccessTokenResponse` method. **With** this change, the Pinterest Provider could simply: ```php protected function getTokenHeaders() { return array_merge( parent::getTokenHeaders(), [ 'Authorization' => 'Basic ' . base64_encode("{$this->clientId}:{$this->clientSecret}"), ] ); } ``` * formatting Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 89151c3 commit ce8b2f9

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/Two/AbstractProvider.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,24 @@ protected function hasInvalidState()
287287
public function getAccessTokenResponse($code)
288288
{
289289
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
290-
RequestOptions::HEADERS => ['Accept' => 'application/json'],
290+
RequestOptions::HEADERS => $this->getTokenHeaders($code),
291291
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
292292
]);
293293

294294
return json_decode($response->getBody(), true);
295295
}
296296

297+
/**
298+
* Get the headers for the access token request.
299+
*
300+
* @param string $code
301+
* @return array
302+
*/
303+
protected function getTokenHeaders($code)
304+
{
305+
return ['Accept' => 'application/json'];
306+
}
307+
297308
/**
298309
* Get the POST fields for the token request.
299310
*

0 commit comments

Comments
 (0)