Skip to content

Commit 50c9679

Browse files
Allowing to post credentials for token retrieval either in form-urlencoded or in json.
1 parent da166d0 commit 50c9679

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

Client/OAuthRequestFactory.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function __construct(
2828
protected string $baseUrl,
2929
protected string $tokenRequestPath,
3030
protected array $authenticationParams,
31+
protected string $tokenRequestContentType = 'application/json',
3132
) {
3233
}
3334

@@ -74,8 +75,19 @@ protected function updateToken(RequestInterface $request, callable $method): OAu
7475

7576
protected function createTokenRequest(): RequestInterface
7677
{
77-
return $this->requestFactory->createRequest('POST', $this->baseUrl.$this->tokenRequestPath)
78-
->withHeader('Content-Type', 'application/x-www-form-urlencoded')
79-
->withBody(Stream::create(http_build_query($this->authenticationParams)));
78+
$tokenRequest = $this->requestFactory->createRequest('POST', $this->baseUrl.$this->tokenRequestPath)
79+
->withHeader('Content-Type', $this->tokenRequestContentType);
80+
81+
if ('application/x-www-form-urlencoded' === $this->tokenRequestContentType) {
82+
$content = http_build_query($this->authenticationParams);
83+
} elseif ('application/json' === $this->tokenRequestContentType) {
84+
$content = json_encode($this->authenticationParams, JSON_THROW_ON_ERROR);
85+
} else {
86+
throw new \UnexpectedValueException(
87+
"Unsupported token request content type {$this->tokenRequestContentType}"
88+
);
89+
}
90+
91+
return $tokenRequest->withBody(Stream::create($content));
8092
}
8193
}

0 commit comments

Comments
 (0)