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.
Deprecated since version 7.4.0: Tokens managers have been deprecated and will be removed in the near future.
- class praw.util.token_manager.BaseTokenManager#
An abstract class for all token managers.
- __init__()#
Initialize a
BaseTokenManagerinstance.
- abstract post_refresh_callback(authorizer)#
Handle callback that is invoked after a refresh token is used.
- Parameters:
authorizer – The
prawcore.Authorizerinstance used containingaccess_tokenandrefresh_tokenattributes.
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.Authorizerinstance used containingaccess_tokenandrefresh_tokenattributes.
This callback can be used to inspect and modify the attributes of the
prawcore.Authorizerinstance, 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_tokenis created prior to use.Warning
The same
fileshould not be used by more than one instance of this class concurrently. Doing so may result in data corruption. Consider usingSQLiteTokenManagerif you want more than one instance of PRAW to concurrently manage a specificrefresh_tokenchain.- __init__(filename)#
Initialize a
FileTokenManagerinstance.- 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, 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, key)#
Initialize a
SQLiteTokenManagerinstance.- Parameters:
database – The path to the SQLite database.
key – The key used to locate the refresh token. This
keycan be anything. You might use theclient_idif you expect to have unique a refresh token for eachclient_id, or you might use a redditor’susernameif you’re managing multiple users’ authentications.
- is_registered()#
Return whether or not
keyalready 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:
Trueifrefresh_tokenis saved to the database, otherwise,Falseif there is already arefresh_tokenfor the associatedkey.