Comments (2)
Pull request: #70
from oauth-subscriber.
I ran into the same issue today but for POST parameters with multiple values. For anyone experiencing this as well, here's how I fixed it.
1. Override the createBaseString
method
Create a new class that extends the Oauth1
class from this package, and override the createBaseString
method (which is only protected
, not private
) to use Query::build()
instead of http_build_query()
. This way the repeated parameters are encoded without []
.
<?php
namespace YourNamespace;
use GuzzleHttp\Psr7\Query;
use Psr\Http\Message\RequestInterface;
final class OAuth1 extends GuzzleHttp\Subscriber\Oauth\Oauth1
{
protected function createBaseString(RequestInterface $request, array $params): string
{
$url = (string) $request->getUri()->withQuery('');
$query = Query::build($params);
return strtoupper($request->getMethod())
. '&' . rawurlencode($url)
. '&' . rawurlencode($query);
}
}
Note: This may not work if you are still using Guzzle v6. In that case you can try the older \GuzzleHttp\Psr7\build_query()
function instead of Query::build()
but I cannot confirm that it fixes the issue.
Use this child class instead of GuzzleHttp\Subscriber\Oauth\Oauth1
as middleware in the Guzzle HandlerStack
.
2. Build the POST body yourself instead of using form_params
If you're sending a POST request with content-type application/x-www-form-urlencoded
and you're using the form_params
option to define the form data, Guzzle will encode it with []
for repeated parameters.
To avoid this, use the body
option instead and encode the POST data yourself using Query::build(...)
. See https://stackoverflow.com/questions/69411391/is-there-a-way-to-prevent-guzzle-from-appending-to-field-names-with-multiple
For example:
$formData = [
'parameter' => ['value1', 'value2'],
];
$options = [
'headers' => ['content-type' => 'application/x-www-form-urlencoded'],
'body' => Query::build($formData),
];
$response = $this->httpClient->request('POST', 'https://...', $options);
3. Make sure parameters with multiple values are sorted by their string value
In my case I had to send multiple integer values for the same parameter. To make sure the generated signature is correct, you need to sort them by their string value.
For example:
$parameter = [3, 222, 10];
sort($parameter, SORT_STRING); // Result: [10, 222, 3];
$formData = ['parameter' => $p];
$options = [
'headers' => ['content-type' => 'application/x-www-form-urlencoded'],
'body' => Query::build($formData),
];
$response = $this->httpClient->request('POST', 'https://...', $options);
from oauth-subscriber.
Related Issues (20)
- Please tag a recent 0.2.1 release HOT 4
- trailing slash for base_uri HOT 1
- Memory leak HOT 2
- Guzzel not able to see the nonce and timestamp HOT 1
- Why this repo seems to be dead? HOT 2
- Notice: Undefined index: token_secret in guzzlehttp/oauth-subscriber/src/Oauth1.php at line 243 HOT 1
- Rest authentication error : Invalid signature - provided signature does not match
- Allow override of token and secret on a per-call basis. HOT 1
- Incorrect signature generated HOT 1
- Problem with signatures for query strings without values HOT 1
- New release
- OAuth 1.0 Two-Legged HOT 1
- Guzzle 7 support
- PHP8 openssl_free_key deprecated HOT 1
- Can I use OAuth1 with hmac-sha256 signature method? HOT 3
- Attempted to call function "parse_query" from namespace "GuzzleHttp\Psr7". HOT 4
- Incompatible dependency. Use of GuzzleHttp\Psr7\parse_query() HOT 3
- Depricated function was removed HOT 1
- Error: Call to undefined function GuzzleHttp\Psr7\parse_query() HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oauth-subscriber.