Password-based authentication for bots is leaving on August 03, 2015.
Ingredients: 1x Web Browser; 1x Folder; 1x Commandline interface; 1x Text Editor. Preheat keyboard to 425°F (400°F for dark or nonstick keyboards).
Open https://www.reddit.com/prefs/apps/ and create an app.
Enter the app info
Click "create app"
Create a new py file and open it in an editor. I will call it "obot.py"
Underneath your app's name is a string of letters, copy that and add it to obot as app_id = 'xxxxxxxxxxxxxx'
Below that is the Secret, copy it as app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
Copy the uri as app_uri = 'https://127.0.0.1:65010/authorize_callback'
Write a useragent for your bot. This is the same as usual. Add it as app_ua = 'blah blah blah'
Decide which scopes you want your bot to have. In this case, I want it to have completel control, so I will use all of the scopes. Save this to obot as
app_scopes = 'account creddits edit flair history identity livemanage modconfig modcontributors modflair modlog modothers modposts modself modwiki mysubreddits privatemessages read report save submit subscribe vote wikiedit wikiread'
(all the desired scopes on a single line, separated by spaces)
Open a commandline to the folder where you created obot. Start the python interpreter
Do this:
import praw
import obot
r = praw.Reddit(obot.app_ua)
r.set_oauth_app_info(obot.app_id, obot.app_secret, obot.app_uri)
r.get_authorize_url('...', obot.app_scopes, True)
# ^ ^ ^
# a b c
a = the "state". This is just a string that's unique to the client and doesn't matter for our purpose
b = the scopes the bot will have
c = whether or not this token will be refreshable. If False, you would have to get a new token each time the current one expires. Since this is a personal use script and I want to make the bot behave like we're used to, I want the token to be infinitely reusable, so it basically functions as a password.
This will return a long URL. Copy it from your command line, fix it in your text editor so it's all on one line and doesn't have the quotation marks around it, and paste it into your browser.
This will open a page that says "OAuth2 App requests acess to your account" with bullet points representing each scope. The bullet at the bottom should say "maintain this access indefinitely" if you used refreshable=True in step 11.
Click allow
This will bring you to a broken webpage because it's trying to access the server at 127.0.0.1, which doesn't exist because you probably aren't running any webservers. In the URL for the page, you should see &code=xxxxxxxxxxxxxx. Copy the code and save it to obot as app_account_code = 'xxxxxxxxxxxxxx'
Go back to your python interpreter, and enter r.get_access_information('xxxxxxxxxxxxxx') using the new account code
This will spit out a dictionary containing your currently active access_token, all of the scopes that you are authorized to use, and your refresh_token
Copy the refresh_token's value, and save it to obot as app_refresh = 'xxxxxxxx-xxxxxxxxxxxxxx'
In python, enter r.refresh_access_information('xxxxxxxx-xxxxxxxxxxxxxx') using the refresh_token.
This should spit out a dictionary that looks the same as the other one.
Close python, then reopen it
Do this:
import praw
import obot
r = praw.Reddit(obot.app_ua)
r.set_oauth_app_info(obot.app_id, obot.app_secret, obot.app_uri)
r.refresh_access_information(obot.app_refresh)
If this works, try playing with a couple of PRAW functions. You should be signed in and have access to all of the methods under the scopes that you requested.
In order to cut down on typing, I like to make a function inside obot that looks like this:
import praw
def login():
r = praw.Reddit(app_ua)
r.set_oauth_app_info(app_id, app_secret, app_uri)
r.refresh_access_information(app_refresh)
return r
Now, in python you can simply say import praw, obot and r = obot.login() and you will be authorized.
You may move obot.py to your Python Lib folder, so that you can import it no matter where you are or what bot you are running.
Congratulations.