API Reference

PrawOAuth2Server

class prawoauth2.PrawOAuth2Server(reddit_client, app_key, app_secret, state, redirect_url='http://127.0.0.1:65010/authorize_callback', scopes=['identity', 'read'], refreshable=True)

Creates an instance of PrawOAuth2Server which is responsible for getting access_token and refresh_token given valid app_key and app_secret. This is meant to be run once only.

Parameters:
  • reddit_client – An Instance of praw
  • app_key – App Secret (or also known as Client Id) of your app. Find them here: https://www.reddit.com/prefs/apps/
  • app_secret – App Key (or also known as Client Secret) of your app. Find them here: https://www.reddit.com/prefs/apps/
  • state – Some unique string which represents your client. You could use user_agent which you used when creating the praw instance.
  • scopes – List of scopes for OAuth. Default is [‘identity’]. https://praw.readthedocs.org/en/latest/pages/oauth.html#oauth-scopes
  • redirect_url – Redirect URL used in authorization process using PrawOAuth2Server. Default is http://127.0.0.1:9999/authorize_callback (which is recommended by praw).
  • refreshable – Boolean. Specifies whether you want access_token to be refreshable or not. If it is set to False then you have to use PrawOAuth2Server again to generate new access_token. Default is True.
get_access_codes()

Returns the access_token and refresh_token. Obviously, this method should be called after start.

Returns:A dictionary containing access_token and refresh_token.
start()

Starts the PrawOAuth2Server server. It will open the default web browser and it will take you to Reddit’s authorization page, asking you to authorize your Reddit account(or account of the bot’s) with your app(or bot script). Once authorized successfully, it will show successful message in web browser.

PrawOAuth2Mini

class prawoauth2.PrawOAuth2Mini(reddit_client, app_key, app_secret, access_token, refresh_token, scopes=['identity', 'read'], redirect_url='http://127.0.0.1:9999/authorize_callback')

Creates a PrawOAuth2Mini instance. PrawOAuth2Mini meant to be used in the bot and it needs valid access_token and refresh_token to operate. Once the access_token is expired, it will be refreshed using the refresh_token

Parameters:
  • reddit_client – An Instance of praw
  • app_key – App Secret (or also known as Client Id) of your app. Find them here: https://www.reddit.com/prefs/apps/
  • app_secret – App Key (or also known as Client Secret) of your app. Find them here: https://www.reddit.com/prefs/apps/
  • access_token – Once you have authorized your Reddit account with the app/bot/script using PrawOAuth2Server, you get a valid access_token (which expires after 60 minutes).
  • refresh_token – Once you have authorized your Reddit account with the app/bot/script using PrawOAuth2Server, you get a valid refresh_token.
  • scopes – List of scopes for OAuth. Default is [‘identity’, ‘read’]. https://praw.readthedocs.org/en/latest/pages/oauth.html#oauth-scopes
  • redirect_url – Redirect URL used in authorization process using PrawOAuth2Server. Default is http://127.0.0.1:9999/authorize_callback (which is recommended by praw).

Make sure you provide same scopes and redirect_url which you used with PrawOAuth2Server.

get_access_codes()

Returns the access_token and refresh_token.

Returns:A dictionary containing access_token and refresh_token.
refresh(force=False)

Refreshes the access_token and sets the praw instance reddit_client with a valid one.

Parameters:force – Boolean. Refresh will be done only when last refresh was done before EXPIRY_DURATION, which is 3500 seconds. However passing force will overrides this and refresh operation will be done everytime.