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

Constants

ORIGIN_CHILD  : mixed = 'child'
ORIGIN_OAUTH2  : mixed = 'oauth2'
ORIGIN_PROVISIONAL_BOT  : mixed = 'provisional_bot'
ORIGIN_PROVISIONAL_EXTERNAL  : mixed = 'provisional_external'
ORIGIN_PROVISIONAL_PUBLIC  : mixed = 'provisional_public'
ORIGIN_UNKNOWN  : mixed = 'unknown'
Origin is local metadata, never inferred from a token response or its scopes.

Properties

$access_token  : string
$expires_at  : ?int
$id_token  : ?string
$origin  : 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>
requiresReacquisition()  : bool
Whether renewal must repeat the original issuance method, instead of OAuth refresh.

Constants

ORIGIN_PROVISIONAL_BOT

public mixed ORIGIN_PROVISIONAL_BOT = 'provisional_bot'

ORIGIN_PROVISIONAL_EXTERNAL

public mixed ORIGIN_PROVISIONAL_EXTERNAL = 'provisional_external'

ORIGIN_PROVISIONAL_PUBLIC

public mixed ORIGIN_PROVISIONAL_PUBLIC = 'provisional_public'

ORIGIN_UNKNOWN

Origin is local metadata, never inferred from a token response or its scopes.

public mixed ORIGIN_UNKNOWN = 'unknown'

Properties

$expires_at read-only

public ?int $expires_at = null

$origin read-only

public string $origin = self::ORIGIN_UNKNOWN

$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 ][, string $origin = self::ORIGIN_UNKNOWN ]) : mixed
Parameters
$access_token : string

The token itself.

$token_type : string = 'Bearer'

Always Bearer for a user's token.

$refresh_token : string|null = null

Present in some provisional responses too; its presence alone does not permit OAuth refresh.

$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.

$origin : string = self::ORIGIN_UNKNOWN

Local issuance provenance; unknown preserves legacy refresh behavior.

__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 ][, string $origin = self::ORIGIN_UNKNOWN ]) : 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.

$origin : string = self::ORIGIN_UNKNOWN

Local provenance supplied by the issuer, not by the response.

Tags
throws
InvalidArgumentException

The response carries no access token or the origin is invalid.

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>

requiresReacquisition()

Whether renewal must repeat the original issuance method, instead of OAuth refresh.

public requiresReacquisition() : bool
Return values
bool
On this page

Search results