{"id":15331265,"url":"https://github.com/wohali/oauth2-discord-new","last_synced_at":"2025-04-04T20:15:37.966Z","repository":{"id":48914449,"uuid":"106991699","full_name":"wohali/oauth2-discord-new","owner":"wohali","description":"New Discord Provider for the OAuth 2.0 Client","archived":false,"fork":false,"pushed_at":"2024-06-30T13:21:49.000Z","size":16,"stargazers_count":119,"open_issues_count":5,"forks_count":21,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-03-28T19:11:22.983Z","etag":null,"topics":["discord","oauth","oauth2","oauth2-client","php"],"latest_commit_sha":null,"homepage":null,"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/wohali.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-15T07:15:21.000Z","updated_at":"2025-02-02T03:34:36.000Z","dependencies_parsed_at":"2024-10-15T23:02:37.548Z","dependency_job_id":"b343d2de-1783-4702-a159-704024a91745","html_url":"https://github.com/wohali/oauth2-discord-new","commit_stats":{"total_commits":15,"total_committers":10,"mean_commits":1.5,"dds":0.8,"last_synced_commit":"ac317d052e031d5d0f72cb2456237a715d644a52"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wohali%2Foauth2-discord-new","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wohali%2Foauth2-discord-new/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wohali%2Foauth2-discord-new/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wohali%2Foauth2-discord-new/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wohali","download_url":"https://codeload.github.com/wohali/oauth2-discord-new/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247242681,"owners_count":20907134,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["discord","oauth","oauth2","oauth2-client","php"],"created_at":"2024-10-01T09:55:08.655Z","updated_at":"2025-04-04T20:15:37.946Z","avatar_url":"https://github.com/wohali.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Discord Provider for OAuth 2.0 Client\n[![Source Code](http://img.shields.io/badge/source-wohali/oauth2--discord--new-blue.svg?style=flat-square)](https://github.com/wohali/oauth2-discord-new)\n[![Latest Version](https://img.shields.io/github/release/wohali/oauth2-discord-new.svg?style=flat-square)](https://github.com/wohali/oauth2-discord-new/releases)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)\n[![Build Status](https://img.shields.io/travis/wohali/oauth2-discord-new/master.svg?style=flat-square)](https://travis-ci.org/wohali/oauth2-discord-new)\n[![Scrutinizer](https://img.shields.io/scrutinizer/g/wohali/oauth2-discord-new/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/wohali/oauth2-discord-new)\n[![Coverage Status](https://img.shields.io/coveralls/wohali/oauth2-discord-new/master.svg?style=flat-square)](https://coveralls.io/r/wohali/oauth2-discord-new?branch=master)\n[![Total Downloads](https://img.shields.io/packagist/dt/wohali/oauth2-discord-new.svg?style=flat-square)](https://packagist.org/packages/wohali/oauth2-discord-new)\n\nThis package provides Discord OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client), v2.0 and up.\n\n## Requirements\n\nThe following versions of PHP are supported.\n\n* PHP 7.2\n* PHP 7.3\n* PHP 7.4\n* PHP 8.0\n* PHP 8.1\n* PHP 8.2\n\n## Installation\n\nTo install, use composer:\n\n```bash\n$ composer require wohali/oauth2-discord-new\n```\n\n## Usage\n\nUsage is the same as The League's OAuth client, using `\\Wohali\\OAuth2\\Client\\Provider\\Discord` as the provider.\n\n### Sample Authorization Code Flow\n\nThis self-contained example:\n\n1. Gets an authorization code\n1. Gets an access token using the provided authorization code\n1. Looks up the user's profile with the provided access token\n\nYou can try this script by [registering a Discord App](https://discord.com/developers/applications/me/create) 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.\n\n```php\n\u003c?php\n\nrequire __DIR__ . '/vendor/autoload.php';\n\nsession_start();\n\necho ('Main screen turn on!\u003cbr/\u003e\u003cbr/\u003e');\n\n$provider = new \\Wohali\\OAuth2\\Client\\Provider\\Discord([\n    'clientId' =\u003e '{discord-client-id}',\n    'clientSecret' =\u003e '{discord-client-secret}',\n    'redirectUri' =\u003e '{your-server-uri-to-this-script-here}'\n]);\n\nif (!isset($_GET['code'])) {\n\n    // Step 1. Get authorization code\n    $authUrl = $provider-\u003egetAuthorizationUrl();\n    $_SESSION['oauth2state'] = $provider-\u003egetState();\n    header('Location: ' . $authUrl);\n\n// Check given state against previously stored one to mitigate CSRF attack\n} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {\n\n    unset($_SESSION['oauth2state']);\n    exit('Invalid state');\n\n} else {\n\n    // Step 2. Get an access token using the provided authorization code\n    $token = $provider-\u003egetAccessToken('authorization_code', [\n        'code' =\u003e $_GET['code']\n    ]);\n\n    // Show some token details\n    echo '\u003ch2\u003eToken details:\u003c/h2\u003e';\n    echo 'Token: ' . $token-\u003egetToken() . \"\u003cbr/\u003e\";\n    echo 'Refresh token: ' . $token-\u003egetRefreshToken() . \"\u003cbr/\u003e\";\n    echo 'Expires: ' . $token-\u003egetExpires() . \" - \";\n    echo ($token-\u003ehasExpired() ? 'expired' : 'not expired') . \"\u003cbr/\u003e\";\n\n    // Step 3. (Optional) Look up the user's profile with the provided token\n    try {\n\n        $user = $provider-\u003egetResourceOwner($token);\n\n        echo '\u003ch2\u003eResource owner details:\u003c/h2\u003e';\n        printf('Hello %s#%s!\u003cbr/\u003e\u003cbr/\u003e', $user-\u003egetUsername(), $user-\u003egetDiscriminator());\n        var_export($user-\u003etoArray());\n\n    } catch (Exception $e) {\n\n        // Failed to get user details\n        exit('Oh dear...');\n\n    }\n}\n```\n\n### Managing Scopes\n\nWhen creating your Discord authorization URL in Step 1, you can specify the state and scopes your application may authorize.\n\n```php\n$options = [\n    'state' =\u003e 'OPTIONAL_CUSTOM_CONFIGURED_STATE',\n    'scope' =\u003e ['identify', 'email', '...'] // array or string\n];\n\n$authorizationUrl = $provider-\u003egetAuthorizationUrl($options);\n```\nIf neither are defined, the provider will utilize internal defaults.\n\nAt the time of authoring this documentation, the [following scopes are available](https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes):\n\n- bot\n- connections\n- email\n- identify\n- guilds\n- guilds.join\n- gdm.join\n- messages.read\n- rpc\n- rpc.api\n- rpc.notifications.read\n- webhook.incoming\n\n### Refreshing a Token\n\nYou 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:\n\n```php\n// create $provider as in the initial example\n$existingAccessToken = getAccessTokenFromYourDataStore();\n\nif ($existingAccessToken-\u003ehasExpired()) {\n    $newAccessToken = $provider-\u003egetAccessToken('refresh_token', [\n        'refresh_token' =\u003e $existingAccessToken-\u003egetRefreshToken()\n    ]);\n\n    // Purge old access token and store new access token to your data store.\n}\n```\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// create $provider as in the initial example\ntry {\n\n    // Try to get an access token using the client credentials grant.\n    $accessToken = $provider-\u003egetAccessToken('client_credentials');\n\n} catch (\\League\\OAuth2\\Client\\Provider\\Exception\\IdentityProviderException $e) {\n\n    // Failed to get the access token\n    exit($e-\u003egetMessage());\n\n}\n```\n\n### Bot Authorization\n\nTo authorize a bot, specify a scope of `bot` and set [permissions](https://discord.com/developers/docs/topics/permissions#permissions-bitwise-permission-flags) appropriately:\n\n```php\n// create $provider as in the initial example\n\n$options = [\n    'scope' =\u003e ['bot'],\n    'permissions' =\u003e 1\n];\n\n$authorizationUrl = $provider-\u003egetAuthorizationUrl($options);\n\n// Redirect user to authorization page\nheader('Location: ' . $authUrl);\n```\n\n## Testing\n\n``` bash\n$ ./vendor/bin/parallel-lint src test\n$ ./vendor/bin/phpcs src --standard=psr2 -sp\n$ ./vendor/bin/phpunit --coverage-text\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/wohali/oauth2-discord-new/blob/master/CONTRIBUTING.md) for details.\n\n## Credits\n\n- [Joan Touzet](https://github.com/wohali)\n- [All Contributors](https://github.com/wohali/oauth2-discord-new/contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](https://github.com/wohali/oauth2-discord-new/blob/master/LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwohali%2Foauth2-discord-new","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwohali%2Foauth2-discord-new","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwohali%2Foauth2-discord-new/lists"}