Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/commandstring/discordoauth
A basic way to use discordoauth in php
https://github.com/commandstring/discordoauth
Last synced: 1 day ago
JSON representation
A basic way to use discordoauth in php
- Host: GitHub
- URL: https://github.com/commandstring/discordoauth
- Owner: CommandString
- License: mit
- Created: 2022-12-31T18:14:36.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-26T07:46:41.000Z (almost 2 years ago)
- Last Synced: 2024-04-18T08:22:41.838Z (7 months ago)
- Language: PHP
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# commandstring/discordoauth #
An easier way to get discord OAuth tokens
# Requirements #
* PHP 8.1=<
* Composer 2
* Registered Discord application# Getting Token
You will need to retrieve your discord application's `client_id` and `client_secret`. After retrieving those you can instantiate the `\CommandString\DiscordOAuth\OAuth` class.```php
$OAuth = (new OAuth("http://localhost:8000", $client_id, $client_secret));
```You will then need to define scopes by using the `addScopes` method. You can either supply a scope string or supply a Scopes enum.
```php
$OAuth->addScopes(Scopes::IDENTIFY);
```Afterwards you can then generate a url for the authorize endpoint by invoking the `buildAuthorizeUrl` method.
```php
$authUrl - $OAuth->buildAuthorizeUrl();
```After the client is redirected you can then use the `getToken` method for retrieving the access token.
```php
$tokenRes = $OAuth->getToken($_GET["code"]);
```*You can supply your own `\React\Http\Browser` in the second argument of this method*
`$tokenRes` will be an stdClass with the following structure.
```json
{
"access_token": "",
"expires_in": "",
"refresh_token": "",
"scope": "",
"token_type": ""
}
```You can also refresh your token with the refreshToken method...
```php
$newTokenRes = $OAuth->refreshToken($tokenRes->refresh_token);
```# Getting user
```php
$user = User::getWithToken($tokenRes->token_type, $tokenRes->access_token);
```*Note: All properties of the user are readonly.*