jojihere / oauth-subscriber
Guzzle OAuth 1.0 订阅者
0.3.1
2016-11-22 10:21 UTC
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-14 19:53:12 UTC
README
使用OAuth 1.0签署HTTP请求。请求使用消费者密钥、消费者密钥、OAuth令牌和OAuth密钥进行签名。
本版本仅适用于Guzzle 6.0及更高版本!
安装
该项目可以使用Composer进行安装。在您的composer.json文件中添加以下内容
{ "require": { "guzzlehttp/oauth-subscriber": "0.3.*" } }
使用订阅者
以下是一个示例,展示如何向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来为客户端发送的所有请求设置auth,通过扩展您提供给new Client的数组来实现,auth => oauth。
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');
注意
您可以使用两步OAuth省略token和token_secret选项。
使用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']);