Skip to content

Commit

Permalink
Fixed LinkedIn resource owner to always put Content-Length header i…
Browse files Browse the repository at this point in the history
…n POST reqeusts : cherry pick 859ec55
  • Loading branch information
stloyd authored and Orliaguet Mylene committed Feb 8, 2021
1 parent 74d2c7e commit 7640802
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 6 additions & 4 deletions OAuth/ResourceOwner/AbstractResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,12 @@ protected function httpRequest($url, $content = null, array $headers = [], $meth
$method = null === $content || '' === $content ? 'GET' : 'POST';
}

$headers += array('User-Agent' => 'HWIOAuthBundle (https://github.com/hwi/HWIOAuthBundle)');
if (is_string($content)) {
$headers += array('Content-Length' => (string) strlen($content));
} elseif (is_array($content)) {
$headers += ['User-Agent' => 'HWIOAuthBundle (https://github.com/hwi/HWIOAuthBundle)'];
if (\is_string($content)) {
if (!isset($headers['Content-Length'])) {
$headers += ['Content-Length' => (string) \strlen($content)];
}
} elseif (\is_array($content)) {
$content = http_build_query($content, '', '&');
}

Expand Down
8 changes: 7 additions & 1 deletion OAuth/ResourceOwner/LinkedinResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ protected function doGetTokenRequest($url, array $parameters = [])
*/
protected function httpRequest($url, $content = null, array $headers = [], $method = null)
{
// Linkedin v2 API is supposed to require Content-Type: application/json but it works without
// LinkedIn v2 API is supposed to require Content-Type: application/json but it works without
// and request to get the access token doesn't seems to work with Content-Type: application/json
// so we don't put any Content-Type header.
// Skip the Content-Type header in GenericOAuth2ResourceOwner::httpRequest
//
// LinkedIn API requires to always set Content-Length in POST requests
if ('POST' === $method) {
$headers['Content-Length'] = \is_string($content) ? (string) \strlen($content) : '0';
}

return AbstractResourceOwner::httpRequest($url, $content, $headers, $method);
}

Expand Down

0 comments on commit 7640802

Please sign in to comment.