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 PRAW users will create their own token manager classes suitable for their needs.
See Using and Updating Refresh Tokens for examples on how to leverage these classes.
- class praw.util.token_manager.BaseTokenManager¶
An abstract class for all token managers.
- __init__()¶
Prepare attributes needed by all token manager classes.
- abstract post_refresh_callback(authorizer)¶
Handle callback that is invoked after a refresh token is used.
- Parameters
authorizer – The
prawcore.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
.
- abstract pre_refresh_callback(authorizer)¶
Handle callback that is invoked before refreshing PRAW’s authorization.
- Parameters
authorizer – The
prawcore.Authorizer
instance used containingaccess_token
andrefresh_token
attributes.
This callback can be used to inspect and modify the attributes of the
prawcore.Authorizer
instance, such as setting therefresh_token
.
- class praw.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 usingSQLiteTokenManager
if you want more than one instance of PRAW to concurrently manage a specificrefresh_token
chain.- __init__(filename)¶
Load and save refresh tokens from a file.
- Parameters
filename – The file the contains the refresh token.
- post_refresh_callback(authorizer)¶
Update the saved copy of the refresh token.
- pre_refresh_callback(authorizer)¶
Load the refresh token from the file.
- class praw.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, initialrefresh_tokens
will need to be registered viaregister()
prior to use. See SQLiteTokenManager for an example of use.Warning
This class is untested on Windows because we encountered file locking issues in the test environment.
- __init__(database, key)¶
Load and save refresh tokens from a SQLite database.
- Parameters
database – The path to the SQLite database.
key – The key used to locate the
refresh_token
. Thiskey
can be anything. You might use theclient_id
if you expect to have uniquerefresh_tokens
for eachclient_id
, or you might use a Redditor’susername
if you’re manage multiple users’ authentications.
- is_registered()¶
Return whether or not
key
already has arefresh_token
.
- post_refresh_callback(authorizer)¶
Update the refresh token in the database.
- pre_refresh_callback(authorizer)¶
Load the refresh token from the database.
- register(refresh_token)¶
Register the initial refresh token in the database.
- Returns
True
ifrefresh_token
is saved to the database, otherwise,False
if there is already arefresh_token
for the associatedkey
.