Migration from HTTP to OAuth2¶
If you have an existing Reddit bot and if you wish to migrate to OAuth2, follow the simple steps.
praw.login. Your current code probably uses Reddit account (your’s or your bot’s) username and password with
praw.loginand you have to remove that. With OAuth2, there should be NO references to Reddit username and password in your code:
reddit_client = praw.Reddit(user_agent=user_agent) reddit_client.login(reddit_username, reddit_password)
Figure out what all
scopesyou need. Scopes specify what all permissions your app (or bot script) needs from user’s Reddit account(or your bot account), like read private messages, spend gold credits etc. You can read about different scopes on praw’s official documentation. For example, if your bot replies to comments and also responds to private messages, then it will need atleast these scopes:
scopes = ['identity', 'read', 'submit', 'privatemessages']
onetime.pyscript. As name suggests, you need to run this script only once for the first time. You should run this script locally, on your computer. Refer to Running PrawOAuth2Server in
User Guide. Once done, you will get
app_secretin your existing code to create a new instance of
PrawOAuth2Mini. Refer to Using PrawOAuth2Mini in
reddit_client = praw.Reddit(user_agent=user_agent) oauth_helper = PrawOAuth2Mini(reddit_client, app_key=app_key, app_secret=app_secret, access_token=access_token, refresh_token=refresh_token, scopes=scopes)
That’s all! Now rest of your code would require no changes and it will work as usual.