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: asyncprawcore.auth.BaseAuthorizer)¶
Handle callback that is invoked after a refresh token is used.
- Parameters:
authorizer – The
asyncprawcore.Authorizer
instance used containingaccess_token
andrefresh_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: asyncprawcore.auth.BaseAuthorizer)¶
Handle callback that is invoked before refreshing PRAW’s authorization.
- Parameters:
authorizer – The
asyncprawcore.Authorizer
instance used containingaccess_token
andrefresh_token
attributes.
This callback can be used to inspect and modify the attributes of the
asyncprawcore.Authorizer
instance, such as setting therefresh_token
.
- property reddit: asyncpraw.Reddit¶
Return the
Reddit
instance bound to the token manager.
- class asyncpraw.util.token_manager.FileTokenManager(filename: str)¶
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 usingSQLiteTokenManager
if you want more than one instance of PRAW to concurrently manage a specificrefresh_token
chain.- __init__(filename: str)¶
Initialize a
FileTokenManager
instance.- Parameters:
filename – The file the contains the refresh token.
- await post_refresh_callback(authorizer: asyncprawcore.auth.BaseAuthorizer)¶
Update the saved copy of the refresh token.
- await pre_refresh_callback(authorizer: asyncprawcore.auth.BaseAuthorizer)¶
Load the refresh token from the file.
- property reddit: asyncpraw.Reddit¶
Return the
Reddit
instance bound to the token manager.
- class asyncpraw.util.token_manager.SQLiteTokenManager(*, database: str, key: str)¶
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 viaregister()
prior to use.Warning
This class is untested on Windows because we encountered file locking issues in the test environment.
- __init__(*, database: str, key: str)¶
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 theclient_id
if you expect to have unique a refresh token for eachclient_id
, or you might use a redditor’susername
if you’re managing multiple users’ authentications.
- await close()¶
Close the sqlite3 connection.
- async with connection()¶
Asynchronously setup and provide the sqlite3 connection.
- await post_refresh_callback(authorizer: asyncprawcore.auth.BaseAuthorizer)¶
Update the refresh token in the database.
- await pre_refresh_callback(authorizer: asyncprawcore.auth.BaseAuthorizer)¶
Load the refresh token from the database.
- property reddit: asyncpraw.Reddit¶
Return the
Reddit
instance bound to the token manager.