romitou / oauth2-discord
此包提供Discord的OAuth 2支持,并兼容League的OAuth 2.0客户端。
1.0.1
2021-01-21 22:05 UTC
Requires
- league/oauth2-client: ^2.6
This package is auto-updated.
Last update: 2024-09-19 23:01:42 UTC
README
此包提供Discord的OAuth 2支持,并兼容League的OAuth 2.0客户端。
🚀 下载和安装
此包可在Packagist上找到。您可以直接使用Composer命令composer req romitou/oauth2-discord
进行安装。
📖 示例
<?php require __DIR__ . '/vendor/autoload.php'; session_start(); $discordProvider = new \Romitou\OAuth2\Client\DiscordProvider([ 'clientId' => '', // The ID of your application 'clientSecret' => '', // The secret of your application 'redirectUri' => '', // The callback URI where the user will be redirected 'scopes' => [], // The scopes you want (e.g. ['identify', 'email']) 'state' => '' // Optional, the state of the provider ]); if (isset($_GET['code'])) { if ($_SESSION['oauth2_state'] !== $_GET['state']) exit('The returned state doesn\'t match with your local state.'); $discordToken = $discordProvider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); $discordUser = $discordProvider->getResourceOwner($discordToken); // Now, you will be able to retrieve user's informations. echo 'Your token is ' . $discordToken->getToken(); echo 'Your username is ' . $discordUser->getUsername(); } else { $oauthUrl = $discordProvider->getAuthorizationUrl(); $_SESSION['oauth2_state'] = $discordProvider->getState(); header('Location: ' . $oauthUrl); }