pandaac/oauth2-otland

此包已被弃用且不再维护。没有建议的替代包。

dev-master 2016-08-23 15:27 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:58:36 UTC


README

Abandoned

OtLand.net OAuth2 客户端

此包允许您的网站用户通过他们的 OtLand.net 账户进行身份验证,然后返回一个包含其 OtLand 详细信息的对象,供您进行相应操作。

要求

  • PHP 5.5.9+
  • OtLand 客户端凭证
    • 获取您自己的客户端 ID & secret 的唯一方式是向 @Mark 发送一条私信请求。您需要向他提供项目名称、简短描述以及重定向 URI。然后由他决定是否授予您必要的凭证。

安装

通过 Composer
composer require pandaac/oauth2-otland

OAuth2 选项

在实例化 pandaac\OAuth2OtLand\Providers\OtLand 对象时,您可以将一个选项数组作为其第一个参数传递。

  • 客户端 ID (必需)
    将您的客户端 ID 分配给 clientId 键。
  • 客户端密钥 (必需)
    将您的客户端密钥分配给 clientSecret 键。
  • 重定向 URI (必需)
    将您的重定向 URI 分配给 redirectUri 键。
  • 作用域
    将您的范围作为数组分配给 scope 键。默认为 ['read']

OtLand.net 使用的 API 是 bdApi。有关简单授权之外的内容,请参阅他们的文档。

示例

Laravel 5.x

将 OtLand OAuth2 客户端服务提供程序添加到 ./config/app.php 中的服务提供程序数组中。

pandaac\OAuth2OtLand\FrameworkIntegration\Laravel\OtLandOAuth2ServiceProvider::class

然后运行以下 artisan 命令发布包配置文件。

php artisan vendor:publish --provider="pandaac\OAuth2OtLand\FrameworkIntegration\Laravel\OtLandOAuth2ServiceProvider"

您可以直接在 ./config/oauth2-otland.php 中编辑您的 OAuth2 凭证,尽管强烈建议通过 .env 文件来编辑(OTLAND_KEYOTLAND_SECRET & OTLAND_REDIRECT)。

并最终定义一个类似以下的路由

use Illuminate\Http\Request;
use pandaac\OAuth2OtLand\Providers\OtLand;

$router->get('/otland', function (Request $request) {
    try {

        $otland = app(OtLand::class);

        // Redirect the user to the authorization url if no code was provided
        if (! $request->has('code')) {
            $url = $otland->getAuthorizationUrl();

            $request->session()->put('oauth2state', $otland->getState());

            return redirect($url);
        }

        // If the state is invalid, redirect the user
        if (! $request->has('state') or ($request->get('state') !== $request->session()->get('oauth2state'))) {
            $request->session()->forget('oauth2state');

            return redirect('/');
        }

        $accessToken = $otland->getAccessToken('authorization_code', [
            'code' => $request->get('code')
        ]);

        $owner = $otland->getResourceOwner($accessToken);

        dd($owner->toArray());

    } catch (Exception $e) {
        // Log errors...
    }
});
无框架
use pandaac\OAuth2OtLand\Providers\OtLand;

session_start();

try {
    
    $otland = new OtLand([
        'clientId'      => 'MY-CLIENT-ID',
        'clientSecret'  => 'MY-CLIENT-SECRET',
        'redirectUri'   => 'MY-REDIRECT-URI',
    ]);

    // Redirect the user to the authorization url if no code was provided
    if (! isset($_GET['code'])) {
        $url = $otland->getAuthorizationUrl();

        $_SESSION['oauth2state'] = $otland->getState();

        header('Location: '.$url);
        exit;
    }

    // If the state is invalid, redirect the user
    if (! isset($_GET['state']) or ($_GET['state'] !== $_SESSION['oauth2state'])) {
        unset($_SESSION['oauth2state']);

        header('Location: /');
        exit;
    }

    $accessToken = $otland->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);

    $owner = $otland->getResourceOwner($accessToken);

    var_dump($owner->toArray());

} catch (Exception $e) {
    // Log errors...
}

贡献

请参考 PSR-2 指南,并在提交拉取请求之前将您的提交合并在一起。

谢谢。