Token Manager

Token Manager classes.

There should be a 1-to-1 mapping between an instance of a subclass of BaseTokenManager and a Reddit instance.

A few proof of concept token manager classes are provided here, but it is expected that Async PRAW users will create their own token manager classes suitable for their needs.

Deprecated since version 7.4.0: Tokens managers have been deprecated and will be removed in the near future.

class asyncpraw.util.token_manager.BaseTokenManager

An abstract class for all token managers.

__init__()

Initialize a BaseTokenManager instance.

abstractmethod post_refresh_callback(authorizer)

Handle callback that is invoked after a refresh token is used.

Parameters:

authorizer – The asyncprawcore.Authorizer instance used containing access_token and refresh_token attributes.

This function will be called after refreshing the access and refresh tokens. This callback can be used for saving the updated refresh_token.

abstractmethod pre_refresh_callback(authorizer)

Handle callback that is invoked before refreshing PRAW’s authorization.

Parameters:

authorizer – The asyncprawcore.Authorizer instance used containing access_token and refresh_token attributes.

This callback can be used to inspect and modify the attributes of the asyncprawcore.Authorizer instance, such as setting the refresh_token.

property reddit

Return the Reddit instance bound to the token manager.

class asyncpraw.util.token_manager.FileTokenManager(filename)

Provides a single-file based token manager.

It is expected that the file with the initial refresh_token is created prior to use.

Warning

The same file should not be used by more than one instance of this class concurrently. Doing so may result in data corruption. Consider using SQLiteTokenManager if you want more than one instance of PRAW to concurrently manage a specific refresh_token chain.

__init__(filename)

Initialize a FileTokenManager instance.

Parameters:

filename – The file the contains the refresh token.

await post_refresh_callback(authorizer)

Update the saved copy of the refresh token.

await pre_refresh_callback(authorizer)

Load the refresh token from the file.

property reddit

Return the Reddit instance bound to the token manager.

class asyncpraw.util.token_manager.SQLiteTokenManager(*, database, key)

Provides a SQLite3 based token manager.

Unlike, FileTokenManager, the initial database need not be created ahead of time, as it’ll automatically be created on first use. However, initial refresh tokens will need to be registered via register() prior to use.

Warning

This class is untested on Windows because we encountered file locking issues in the test environment.

__init__(*, database, key)

Initialize a SQLiteTokenManager instance.

Parameters:
  • database – The path to the SQLite database.

  • key – The key used to locate the refresh token. This key can be anything. You might use the client_id if you expect to have unique a refresh token for each client_id, or you might use a redditor’s username if you’re managing multiple users’ authentications.

await close()

Close the sqlite3 connection.

async with connection()

Asynchronously setup and provide the sqlite3 connection.

await is_registered()

Return whether key already has a refresh_token.

await post_refresh_callback(authorizer)

Update the refresh token in the database.

await pre_refresh_callback(authorizer)

Load the refresh token from the database.

property reddit

Return the Reddit instance bound to the token manager.

await register(refresh_token)

Register the initial refresh token in the database.

Returns:

True if refresh_token is saved to the database, otherwise, False if there is already a refresh_token for the associated key.