DiscordPHP Documentation

AccessToken
in package
implements JsonSerializable

FinalYes

A user's OAuth2 access token, as Discord issued it.

Immutable: refreshing produces a new token rather than changing this one. The expiry is recorded as a timestamp when the token arrives, so it stays correct however long the token is kept.

The secrets are never shown by var_dump() or print_r().

Tags
since
10.59.0
link
https://docs.discord.com/developers/topics/oauth2#authorization-code-grant-access-token-response

Table of Contents

Interfaces

JsonSerializable

Properties

$access_token  : string
$expires_at  : ?int
$id_token  : ?string
$refresh_token  : ?string
$scopes  : array<string|int, mixed>
$token_type  : string

Methods

__construct()  : mixed
__debugInfo()  : array<string, mixed>
authorization()  : string
The value for an `Authorization` header.
fromArray()  : self
Rebuilds a token from what {@see jsonSerialize()} produced, as a token store keeps it.
fromResponse()  : self
Builds a token from a token response, such as the one `oauth2/token` or `partner-sdk/token/bot` returns.
isExpired()  : bool
Whether the token has expired, or will within `$leeway` seconds.
isRefreshable()  : bool
Whether the token can be refreshed through `oauth2/token`.
jsonSerialize()  : array<string, mixed>

Properties

$expires_at read-only

public ?int $expires_at = null

$refresh_token read-only

public ?string $refresh_token = null

$scopes read-only

public array<string|int, mixed> $scopes = []

$token_type read-only

public string $token_type = 'Bearer'

Methods

__construct()

public __construct(string $access_token[, string $token_type = 'Bearer' ][, string|null $refresh_token = null ][, int|null $expires_at = null ][, array<string|int, string> $scopes = [] ][, string|null $id_token = null ]) : mixed
Parameters
$access_token : string

The token itself.

$token_type : string = 'Bearer'

Always Bearer for a user's token.

$refresh_token : string|null = null

What to exchange for a new token when this one expires. Provisional account tokens have none.

$expires_at : int|null = null

When the token expires, as a Unix timestamp.

$scopes : array<string|int, string> = []

The scopes the token was granted.

$id_token : string|null = null

An OpenID Connect ID token, when one was issued.

__debugInfo()

public __debugInfo() : array<string, mixed>
Return values
array<string, mixed>

authorization()

The value for an `Authorization` header.

public authorization() : string
Return values
string

fromArray()

Rebuilds a token from what {@see jsonSerialize()} produced, as a token store keeps it.

public static fromArray(array<string|int, mixed> $stored) : self
Parameters
$stored : array<string|int, mixed>
Return values
self

fromResponse()

Builds a token from a token response, such as the one `oauth2/token` or `partner-sdk/token/bot` returns.

public static fromResponse(object|array<string|int, mixed> $response[, int|null $now = null ]) : self
Parameters
$response : object|array<string|int, mixed>

The decoded response.

$now : int|null = null

The time the response arrived, as a Unix timestamp; now if omitted.

Tags
throws
InvalidArgumentException

The response carries no access token.

Return values
self

isExpired()

Whether the token has expired, or will within `$leeway` seconds.

public isExpired([int $leeway = 60 ][, int|null $now = null ]) : bool

A token without a known expiry is never considered expired.

Parameters
$leeway : int = 60

How early to treat it as expired, so a request made now does not race the expiry.

$now : int|null = null

The current time, as a Unix timestamp; now if omitted.

Return values
bool

isRefreshable()

Whether the token can be refreshed through `oauth2/token`.

public isRefreshable() : bool
Return values
bool

jsonSerialize()

public jsonSerialize() : array<string, mixed>
Return values
array<string, mixed>
On this page

Search results