guzzlehttp / oauth-subscriber
Guzzle OAuth 1.0 订阅者
0.6.0
2021-07-13 12:01 UTC
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: ^6.5|^7.2
- guzzlehttp/psr7: ^1.7|^2.0
Requires (Dev)
- phpunit/phpunit: ~4.0|^9.3.3
Suggests
- ext-openssl: Required to sign using RSA-SHA1
This package is auto-updated.
Last update: 2024-09-18 05:57:33 UTC
README
使用 OAuth 1.0 签名 HTTP 请求。请求通过消费者密钥、消费者密钥、OAuth 令牌和 OAuth 密钥进行签名。
此版本仅适用于 Guzzle 6.0 及以上版本!
安装
可以使用 Composer 安装此项目。将以下内容添加到您的 composer.json 文件中:
{ "require": { "guzzlehttp/oauth-subscriber": "^0.6" } }
使用订阅者
以下是一个示例,展示了如何向 Twitter REST API 发送认证请求:
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use GuzzleHttp\Subscriber\Oauth\Oauth1; $stack = HandlerStack::create(); $middleware = new Oauth1([ 'consumer_key' => 'my_key', 'consumer_secret' => 'my_secret', 'token' => 'my_token', 'token_secret' => 'my_token_secret' ]); $stack->push($middleware); $client = new Client([ 'base_uri' => 'https://api.twitter.com/1.1/', 'handler' => $stack ]); // Set the "auth" request option to "oauth" to sign using oauth $res = $client->get('statuses/home_timeline.json', ['auth' => 'oauth']);
您可以通过将 auth 请求选项设置为 oauth 来使客户端发送的所有请求都使用 OAuth 进行认证,只需将 auth => oauth 添加到您提供给 new Client 的数组中。
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use GuzzleHttp\Subscriber\Oauth\Oauth1; $stack = HandlerStack::create(); $middleware = new Oauth1([ 'consumer_key' => 'my_key', 'consumer_secret' => 'my_secret', 'token' => 'my_token', 'token_secret' => 'my_token_secret' ]); $stack->push($middleware); $client = new Client([ 'base_uri' => 'https://api.twitter.com/1.1/', 'handler' => $stack, 'auth' => 'oauth' ]); // Now you don't need to add the auth parameter $res = $client->get('statuses/home_timeline.json');
注意
您可以将 token 和 token_secret 选项设置为空字符串以使用两腿 OAuth。
使用 RSA-SH1 签名方法
use GuzzleHttp\Subscriber\Oauth\Oauth1; $stack = HandlerStack::create(); $middleware = new Oauth1([ 'consumer_key' => 'my_key', 'consumer_secret' => 'my_secret', 'private_key_file' => 'my_path_to_private_key_file', 'private_key_passphrase' => 'my_passphrase', 'signature_method' => Oauth1::SIGNATURE_METHOD_RSA, ]); $stack->push($middleware); $client = new Client([ 'handler' => $stack ]); $response = $client->get('http://httpbin.org', ['auth' => 'oauth']);