guzzle/plugin-oauth
Guzzle OAuth plugin adding OAuth 1.0 request signing to your HTTP clients. Helps authenticate against APIs requiring OAuth headers or query params by attaching the proper signature to outgoing requests. Suitable for legacy OAuth 1 services.
Installation Add the package via Composer:
composer require guzzle/plugin-oauth
Ensure compatibility with Guzzle 3.x (this package is a subtree split of Guzzle’s OAuth plugin).
Basic Usage Register the OAuth plugin with Guzzle’s client:
use Guzzle\Plugin\OAuth\OAuth1;
use Guzzle\Http\Client;
$client = new Client();
$client->addSubscriber(new OAuth1([
'consumer_key' => 'your_key',
'consumer_secret' => 'your_secret',
'token' => 'user_token',
'token_secret' => 'user_token_secret',
]));
First Request Use the client to make an authenticated request:
$response = $client->get('https://api.example.com/protected-endpoint');
Configuration
Store credentials in .env or a config file:
config([
'services.oauth' => [
'consumer_key' => env('OAUTH_CONSUMER_KEY'),
'consumer_secret' => env('OAUTH_CONSUMER_SECRET'),
'token' => env('OAUTH_TOKEN'),
'token_secret' => env('OAUTH_TOKEN_SECRET'),
],
]);
Dynamic Plugin Attachment Attach the plugin conditionally (e.g., for API services):
$client = new Client();
if ($this->shouldUseOAuth()) {
$client->addSubscriber(new OAuth1(config('services.oauth')));
}
Signing Requests
The plugin automatically signs requests with OAuth1 parameters (e.g., oauth_signature, oauth_nonce).
$client = new Client(['base_url' => 'https://api.example.com']);
$client->addSubscriber(new OAuth1(config('services.oauth')));
Guzzle 3.x Only
This package does not support Guzzle 6/7. Ensure your project uses Guzzle 3.x or migrate to a modern OAuth package (e.g., league/oauth1-client).
Signature Method
OAuth1 requires a signature_method (default: HMAC-SHA1). Explicitly set it if the server expects a different method:
new OAuth1([
'signature_method' => 'PLAINTEXT', // Rare, but possible
]);
Nonce and Timestamp
The plugin auto-generates oauth_nonce and oauth_timestamp, but ensure your server’s clock is synchronized to avoid "stale nonce" errors.
$client->addSubscriber(new \Guzzle\Plugin\Debug\DebugClient());
OAuth1 to modify signature generation:
class CustomOAuth1 extends OAuth1 {
protected function getSignatureMethod() {
return 'CUSTOM_METHOD';
}
}
Guzzle\Plugin\Cache) for caching OAuth-authenticated responses.How can I help you explore Laravel packages today?