thephpleague / oauth2-client

Easy integration with OAuth 2.0 service providers.
http://oauth2-client.thephpleague.com
MIT License
3.66k stars 752 forks source link

Fix: sending JSON or multipart requests #995

Open fverry opened 1 year ago

fverry commented 1 year ago

Sending url-encoded params, json or multipart requests with Guzzle is easy, because shortcuts are implemented within GuzzleHttp\Client::applyOptions().

Currently, those shortcuts are unreachable through the League OAuth2 Client, and thus all its providers.

This fix relies on AbstractProvider::getResponse(), and does not affect AbstractProvider::getAuthenticatedRequest().

GuzzleHttp\ClientInterface::send() method supports a second optional $options parameter since v6.0.

Upgrading AbstractProvider::getResponse() to support this second optional parameter allows the developper to use a "form_params", "json" or "multipart" option key :

$options = ['multipart' => $multipart];
$request = $oauth2client->getAuthenticatedRequest($method, $url, $token);
$response = $oauth2client->getResponse($request, $options);

See also

Fixes: #935 #985 Duplicates: #967


For immediate use

Run the following on composer CLI:

composer config repositories.oauth2-client vcs https://github.com/fverry/oauth2-client
composer require "league/oauth2-client:dev-httpclientinterface-send-options as 2.x-dev"

Workaround

Use:

$request = $oauth2client->getAuthenticatedRequest($method, $url, $token);
$oauth2client->getHttpClient()->send($request, $options);

Instead of:

$request = $oauth2client->getAuthenticatedRequest($method, $url, $token, $options);
$oauth2client->getResponse($request);
ramsey commented 1 year ago

@fverry, thank you for submitting this PR. Please take a look at the failing tests and fix those. Then, we'll accept this.

isleshocky77 commented 11 months ago

I'd note that this is an issue for query parameters as well as using json.

@fverry I'm wondering why you chose this as the fix though. Would it not be better to fix RequestFactory::getRequestWithOptions to use GuzzleHttp\Client::applyOptions() instead of cherry-picking only headers, body, and version?

gavin310 commented 8 months ago

Wow thank god I found this thread because I was having a hell of a time getting POST requests to work. $oauth2client->getHttpClient()->send($request, $options); saved the day.