wohali/oauth2-discord-new
Composer 安装命令:
composer require wohali/oauth2-discord-new
包简介
Discord OAuth 2.0 Client Provider for The PHP League OAuth2-Client
README 文档
README
This package provides Discord OAuth 2.0 support for the PHP League's OAuth 2.0 Client, v2.0 and up.
Requirements
The following versions of PHP are supported.
- PHP 7.2
- PHP 7.3
- PHP 7.4
- PHP 8.0
- PHP 8.1
- PHP 8.2
Installation
To install, use composer:
$ composer require wohali/oauth2-discord-new
Usage
Usage is the same as The League's OAuth client, using \Wohali\OAuth2\Client\Provider\Discord as the provider.
Sample Authorization Code Flow
This self-contained example:
- Gets an authorization code
- Gets an access token using the provided authorization code
- Looks up the user's profile with the provided access token
You can try this script by registering a Discord App with a redirect URI to your server's copy of this sample script. Then, place the Discord app's client id and secret, along with that same URI, into the settings at the top of the script.
<?php require __DIR__ . '/vendor/autoload.php'; session_start(); echo ('Main screen turn on!<br/><br/>'); $provider = new \Wohali\OAuth2\Client\Provider\Discord([ 'clientId' => '{discord-client-id}', 'clientSecret' => '{discord-client-secret}', 'redirectUri' => '{your-server-uri-to-this-script-here}' ]); if (!isset($_GET['code'])) { // Step 1. Get authorization code $authUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authUrl); // Check given state against previously stored one to mitigate CSRF attack } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state'); } else { // Step 2. Get an access token using the provided authorization code $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // Show some token details echo '<h2>Token details:</h2>'; echo 'Token: ' . $token->getToken() . "<br/>"; echo 'Refresh token: ' . $token->getRefreshToken() . "<br/>"; echo 'Expires: ' . $token->getExpires() . " - "; echo ($token->hasExpired() ? 'expired' : 'not expired') . "<br/>"; // Step 3. (Optional) Look up the user's profile with the provided token try { $user = $provider->getResourceOwner($token); echo '<h2>Resource owner details:</h2>'; printf('Hello %s#%s!<br/><br/>', $user->getUsername(), $user->getDiscriminator()); var_export($user->toArray()); } catch (Exception $e) { // Failed to get user details exit('Oh dear...'); } }
Managing Scopes
When creating your Discord authorization URL in Step 1, you can specify the state and scopes your application may authorize.
$options = [ 'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE', 'scope' => ['identify', 'email', '...'] // array or string ]; $authorizationUrl = $provider->getAuthorizationUrl($options);
If neither are defined, the provider will utilize internal defaults.
At the time of authoring this documentation, the following scopes are available:
- bot
- connections
- identify
- guilds
- guilds.join
- gdm.join
- messages.read
- rpc
- rpc.api
- rpc.notifications.read
- webhook.incoming
Refreshing a Token
You can refresh an expired token using a refresh token rather than going through the entire process of obtaining a brand new token. To do so, simply reuse the fresh token from your data store to request a refresh:
// create $provider as in the initial example $existingAccessToken = getAccessTokenFromYourDataStore(); if ($existingAccessToken->hasExpired()) { $newAccessToken = $provider->getAccessToken('refresh_token', [ 'refresh_token' => $existingAccessToken->getRefreshToken() ]); // Purge old access token and store new access token to your data store. }
Client Credentials Grant
Discord provides a client credentials flow for bot developers to get their own bearer tokens for testing purposes. This returns an access token for the bot owner:
// create $provider as in the initial example try { // Try to get an access token using the client credentials grant. $accessToken = $provider->getAccessToken('client_credentials'); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { // Failed to get the access token exit($e->getMessage()); }
Bot Authorization
To authorize a bot, specify a scope of bot and set permissions appropriately:
// create $provider as in the initial example $options = [ 'scope' => ['bot'], 'permissions' => 1 ]; $authorizationUrl = $provider->getAuthorizationUrl($options); // Redirect user to authorization page header('Location: ' . $authUrl);
Testing
$ ./vendor/bin/parallel-lint src test
$ ./vendor/bin/phpcs src --standard=psr2 -sp
$ ./vendor/bin/phpunit --coverage-text
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
wohali/oauth2-discord-new 适用场景与选型建议
wohali/oauth2-discord-new 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 763.99k 次下载、GitHub Stars 达 116, 最近一次更新时间为 2017 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「client」 「oauth」 「oauth2」 「authorisation」 「discord」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wohali/oauth2-discord-new 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wohali/oauth2-discord-new 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wohali/oauth2-discord-new 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WeChat OAuth SDK
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Library for ORCID web services
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
统计信息
- 总下载量: 763.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 117
- 点击次数: 28
- 依赖项目数: 10
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-15