smorken / oauth1

OAuth 1 库,专门为 Brainfuse 构建

v10.1.2 2024-07-31 17:24 UTC

This package is auto-updated.

Last update: 2024-08-31 17:34:38 UTC


README

OAuth1 包

此库专门用于与 Brainfuse 一起使用。它可能没有实现完整 OAuth 服务调用的所有功能。但如果你有创意,也许可以做到!

Laravel

  • 添加到 composer.json 应自动注册服务提供者,如果不是的话

    • \Smorken\OAuth1\ServiceProvider 添加到 config/app.php 服务提供者
  • 如有需要,发布配置

    • php artisan vendor:publish --provider="Smorken\OAuth1\ServiceProvider" --tag=config
  • 环境变量可用于配置中的大多数选项 (.env)

  • 工厂

    • $factory = app(\Smorken\OAuth1\Contracts\Factory::class);

使用

整个过程如下(请求令牌)

$uri = 'http://term.ie/oauth/example/request_token.php';

$opts = [
    'user_id'                          => 'abcd',
    'lis_person_name_fname'            => 'Fname',
    'lis_person_name_lname'            => 'Lname',
    'lis_person_contact_email_primary' => 'email@example.com',
    'oauth_consumer_key'               => 'key,
];

$signer = new Signature(new \Smorken\OAuth1\Hashers\HmacSha1('secret'));

$params = new Params(new Nonce());

$client = new \Smorken\OAuth1\Clients\Guzzle(new \GuzzleHttp\Client());

$factory = new OauthFactory(new Authorization(), $client, $params, $signer);

$resp = $factory->post($uri, $opts, ['debug' => true]);
// OR
//$resp = $factory->get($uri, $opts);
// OR
//$resp = $factory->authorizationHeader($uri, $opts, 'POST');

echo $resp->getBody() . PHP_EOL;

如果默认值不能满足你的需求,你也可以手动遵循工厂中的步骤。

手动构建请求的示例

$uri = 'http://term.ie/oauth/example/request_token.php';

$opts = [
    'user_id'                          => 'abcd',
    'lis_person_name_fname'            => 'Fname',
    'lis_person_name_lname'            => 'Lname',
    'lis_person_contact_email_primary' => 'email@example.com',
    'oauth_consumer_key'               => 'key,
];

$signer = new Signature(new \Smorken\OAuth1\Hashers\HmacSha1('secret'));

$params = new Params(new Nonce());

$client = new \Smorken\OAuth1\Clients\Guzzle(new \GuzzleHttp\Client());

$request = new \GuzzleHttp\Psr7\Request(
    'POST',
    $uri,
    ['Content-Type' => 'application/x-www-form-urlencoded'],
    $params->sign($signer, $uri, 'POST')->toQueryString()
);

$resp = $client->send($request);

echo $resp->getBody() . PHP_EOL;