Migration from HTTP to OAuth2ΒΆ
If you have an existing Reddit bot and if you wish to migrate to OAuth2, follow the simple steps.
Stop using
praw.login
. Your current code probably uses Reddit account (your’s or your bot’s) username and password withpraw.login
and 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
scopes
you 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']
Next you need register your app/script/bot in Reddit. Once done, you will have
app_key
andapp_secret
. For more detailed instruction refer toUser Guide
.Build
onetime.py
script. 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 inUser Guide
. Once done, you will getaccess_token
andrefreh_token
.Now use
app_key
,app_secret
,app_key
andapp_secret
in your existing code to create a new instance ofPrawOAuth2Mini
. Refer to Using PrawOAuth2Mini inUser Guide
.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.