{"id":18254472,"url":"https://github.com/teampanfu/oauth2-discord","last_synced_at":"2026-05-02T06:37:33.276Z","repository":{"id":57673658,"uuid":"482113645","full_name":"teampanfu/oauth2-discord","owner":"teampanfu","description":"Discord Provider for the PHP League's OAuth 2.0 Client","archived":false,"fork":false,"pushed_at":"2023-10-17T23:20:41.000Z","size":80,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-20T00:47:37.442Z","etag":null,"topics":["authentication","authorization","client","discord","oauth","oauth2"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/teampanfu/oauth2-discord","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/teampanfu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-15T23:58:35.000Z","updated_at":"2024-08-17T15:04:44.000Z","dependencies_parsed_at":"2022-08-31T13:10:25.977Z","dependency_job_id":null,"html_url":"https://github.com/teampanfu/oauth2-discord","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/teampanfu/oauth2-discord","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teampanfu%2Foauth2-discord","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teampanfu%2Foauth2-discord/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teampanfu%2Foauth2-discord/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teampanfu%2Foauth2-discord/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/teampanfu","download_url":"https://codeload.github.com/teampanfu/oauth2-discord/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teampanfu%2Foauth2-discord/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32525895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-02T01:12:54.858Z","status":"online","status_checked_at":"2026-05-02T02:00:05.923Z","response_time":132,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","authorization","client","discord","oauth","oauth2"],"created_at":"2024-11-05T10:12:39.779Z","updated_at":"2026-05-02T06:37:33.259Z","avatar_url":"https://github.com/teampanfu.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Discord Provider for OAuth 2.0 Client\n\n[![Latest Version](https://img.shields.io/github/release/teampanfu/oauth2-discord.svg?style=flat-square)](https://github.com/teampanfu/oauth2-discord/releases)\n[![Total Downloads](https://img.shields.io/packagist/dt/teampanfu/oauth2-discord.svg?style=flat-square)](https://packagist.org/packages/teampanfu/oauth2-discord)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n\nThis package provides Discord OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client).\n\n## Installation\n\nTo install, use [Composer](https://getcomposer.org):\n\n```sh\ncomposer require teampanfu/oauth2-discord\n```\n\n## Usage\n\nThe first step in implementing OAuth2 is [registering a developer application](https://discord.com/developers/applications) and retrieving your client ID and client secret.\n\n### Authorization Code Flow\n\n```php\n\u003c?php\n\nrequire __DIR__.'/vendor/autoload.php';\n\nuse Panfu\\OAuth2\\Client\\Provider\\Discord;\n\nsession_start();\n\n$provider = new Discord([\n    'clientId' =\u003e 'YOUR_CLIENT_ID',\n    'clientSecret' =\u003e 'YOUR_CLIENT_SECRET',\n    'redirectUri' =\u003e 'http://localhost/callback-url',\n]);\n\nif (!empty($_GET['error'])) {\n    // Got an error, probably user denied access\n    exit('Got error: '.htmlspecialchars($_GET['error'], ENT_QUOTES, 'UTF-8'));\n} else if (empty($_GET['code'])) {\n    // If we don't have an authorization code then get one\n    $authUrl = $provider-\u003egetAuthorizationUrl();\n    $_SESSION['oauth2state'] = $provider-\u003egetState();\n    header('Location: '.$authUrl);\n    exit;\n} else if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {\n    // State is invalid, possible CSRF attack in progress\n    unset($_SESSION['oauth2state']);\n    exit('Invalid state');\n} else {\n    // Try to get an access token (using the authorization code grant)\n    $token = $provider-\u003egetAccessToken('authorization_code', [\n        'code' =\u003e $_GET['code'],\n    ]);\n\n    // Now that you have a token, you can retrieve a user's data\n    try {\n        $user = $provider-\u003egetResourceOwner($token);\n\n        // Depending on which scope you use, you now have access to the user data\n        printf('Hello %s#%s!', $user-\u003egetUsername(), $user-\u003egetDiscriminator());\n    } catch (Exception $e) {\n        // Failed to get user data\n        exit('Something went wrong:'.$e-\u003egetMessage());\n    }\n}\n```\n\n### Retrieving User Data\n\nWhen using the `getResourceOwner()` method to obtain the user node, it will be returned as a `DiscordUser` entity.\n\n```php\n$user = $provider-\u003egetResourceOwner($token);\n\n$id = $user-\u003egetId();\nvar_dump($id);\n# string(17) \"80351110224678912\"\n\n$username = $user-\u003egetUsername();\nvar_dump($username);\n# string(5) \"Nelly\"\n\n$discriminator = $user-\u003egetDiscriminator();\nvar_dump($discriminator);\n# string(4) \"1337\"\n\n$avatar = $user-\u003egetAvatar();\nvar_dump($avatar);\n# string(32) \"8342729096ea3675442027381ff50dfe\"\n\n$isBot = $user-\u003egetBot();\nvar_dump($isBot);\n# boolean false\n\n$isSystem = $user-\u003egetSystem();\nvar_dump($isSystem);\n# boolean false\n\n$isMfaEnabled = $user-\u003egetMfaEnabled();\nvar_dump($isMfaEnabled);\n# boolean true\n\n$banner = $user-\u003egetBanner();\nvar_dump($banner);\n# string(32) \"06c16474723fe537c283b8efa61a30c8\"\n\n$accentColor = $user-\u003egetAccentColor();\nvar_dump($accentColor);\n# int 16711680\n\n$locale = $user-\u003egetLocale();\nvar_dump($locale);\n# string(5) \"en-GB\"\n\n$verified = $user-\u003egetVerified();\nvar_dump($verified);\n# boolean true\n\n$email = $user-\u003egetEmail();\nvar_dump($email);\n# string(17) \"nelly@discord.com\"\n\n$flags = $user-\u003egetFlags();\nvar_dump($flags);\n# int 64\n\n$premiumType = $user-\u003egetPremiumType();\nvar_dump($premiumType);\n# int 1\n\n$publicFlags = $user-\u003egetPublicFlags();\nvar_dump($publicFlags);\n# int 64\n```\n\nYou can also get all the data from the user node as a plain-old PHP array with `toArray()`.\n\n```php\n$userData = $user-\u003etoArray();\n```\n\n### Managing Scopes\n\nWhen creating the authorization URL, you can specify different scopes.\n\n```php\n$options = [\n    'scope' =\u003e ['identify', 'email', 'guilds.join'],\n];\n\n$authUrl = $provider-\u003egetAuthorizationUrl($options);\n```\n\nA list of [all available scopes](https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes) can be found in the Discord API documentation.\n\n### Client Credentials Grant\n\nDiscord 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:\n\n```php\n$provider = new Discord(...);\n\ntry {\n    $accessToken = $provider-\u003egetAccessToken('client_credentials');\n} catch (Exception $e) {\n    exit('Something went wrong: '.$e-\u003egetMessage());\n}\n```\n\n### Bot Authorization\n\nTo authorize a bot, specify the `bot` scope and set permissions appropriately:\n\n```php\n$provider = new Discord(...);\n\n$options = [\n    'scope' =\u003e ['bot'],\n    'permissions' =\u003e 1,\n];\n\n$authUrl = $provider-\u003egetAuthorizationUrl($options);\n\nheader('Location: '.$authUrl);\n```\n\n## Testing\n\n```sh\n$ ./vendor/bin/phpunit\n```\n\n## Contribute\n\nIf you find a bug or have a suggestion for a feature, feel free to create a new issue or open a pull request.\n\nWe are happy about every contribution!\n\n## License\n\nThis package is open-source software licensed under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteampanfu%2Foauth2-discord","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteampanfu%2Foauth2-discord","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteampanfu%2Foauth2-discord/lists"}