Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OAuth support; update README #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 49 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ This will create the following wiki pages (customizable in default_wiki_config.j
# The imported config from default_wiki_config.json
/wiki/twitchbot_config

# A list of banned twitch.tv usernames seperated by newlines
# A list of banned twitch.tv usernames separated by newlines
/wiki/banned_streams

# A list of twitch.tv usernames seperated by newlines
# A list of twitch.tv usernames separated by newlines
/wiki/streams

setup.py will also print out a string for you to put into your sidebar to allow people to PM the bot their livestreams in the correct format:
Expand All @@ -28,33 +28,59 @@ setup.py will also print out a string for you to put into your sidebar to allow

with your bots username and the subreddit it's running in (taken from config.py) substituted where marked.

### OAuth configuration
The use of OAuth in PRAW requires a praw.ini file located in your config folder (varies by platform) or in the root directory if this project (overrides global settings)

No matter where you put it, you need a praw.ini file with the following included:
```
[DEFAULT]
domain: www.reddit.com
oauth_client_id: <client_id>
oauth_client_secret: <secret_access_key>
oauth_redirect_uri: http://127.0.0.1:65010/authorize_callback
oauth_refresh_token: <refresh_token>
```
#### Authenticating with OAuth

There a few steps to authenticate via OAuth:
1. Get the `client_id` and `secret_access_key` from [Reddit](https://www.reddit.com/prefs/apps/)
2. Copy `client_id` to praw.ini as `oath_client_id` and `secret_access_key` as `oauth_client_secret` in praw.ini
3. Run `authenticate.py`. It will open up your default web browser and present a page to grant access to the application
4. You will be redirected to the url at `oauth_redirect_uri` in praw.ini. If you leave the default value, you will likely get a page not found error. This is okay. All you need is the code at the end of URL: `http://127.0.0.1:65010/authorize_callback?state=obtainingAuthentication&code=THIS_IS_THE_CODE_YOU_WANT`
5. Copy the code into the prompt and press enter
6. The script gets the refresh token and prints it to stdout. Copy the refresh token to praw.ini as `oauth_refresh_token`

Now you will be good to go! You will not have to re-run authenticate.py unless you de-authorize your application from Reddit or you allow your refresh token to expire.

See [here](http://praw.readthedocs.org/en/latest/pages/oauth.html) for more details on the PRAW implementation of OAuth


###twitchbot_config

All of the following config is editable in default_wiki_config.json before you run setup.py, and after you've ran setup.py the bot will pull it from `/wiki/twitchbot_config`

{
"max_streams_displayed":"12",
"max_title_length":"50",
"thumbnail_size":{
"width":"80",
"height":"50"
},
"stream_marker_start":"[](#startmarker)",
"stream_marker_end":"[](#endmarker)",
"string_format":"> 1. **[{name}](http://twitch.tv/{name})** -**{viewercount} Viewers**\n[{title}](http://twitch.tv/{name})\n",
"no_streams_string":"**No streams are currently live.**\n",
"wikipages":{
"error_log":"twitchbot_error_log",
"stream_list":"streams",
"ban_list":"banned_streams"
},
"allowed_games":[],
"messages":{
"success":"Your stream will be added to the list of livestreams in the sidebar, it will display the next time you are live on twitch.tv.\n\nProblems? [Contact the moderators here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message.",
"banned":"Sorry, but that stream is banned from this subreddit. If you feel this is an incorrect ban, [please message the moderators here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message.",
"already_exists":"Your stream is already in the list of livestreams that this bot checks. If you have just messaged your stream, please wait 5-10 minutes for the sidebar to update.\n\n Problems? Contact the moderators [here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message."
}
"max_streams_displayed":"12",
"max_title_length":"50",
"thumbnail_size":{
"width":"80",
"height":"50"
},
"stream_marker_start":"[](#startmarker)",
"stream_marker_end":"[](#endmarker)",
"string_format":"> 1. **[{name}](http://twitch.tv/{name})** -**{viewercount} Viewers**\n[{title}](http://twitch.tv/{name})\n",
"no_streams_string":"**No streams are currently live.**\n",
"wikipages":{
"error_log":"twitchbot_error_log",
"stream_list":"streams",
"ban_list":"banned_streams"
},
"allowed_games":[],
"messages":{
"success":"Your stream will be added to the list of livestreams in the sidebar, it will display the next time you are live on twitch.tv.\n\nProblems? [Contact the moderators here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message.",
"banned":"Sorry, but that stream is banned from this subreddit. If you feel this is an incorrect ban, [please message the moderators here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message.",
"already_exists":"Your stream is already in the list of livestreams that this bot checks. If you have just messaged your stream, please wait 5-10 minutes for the sidebar to update.\n\n Problems? Contact the moderators [here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message."
}
}

### Config information
Expand Down
11 changes: 11 additions & 0 deletions authenticate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import praw, webbrowser

r = praw.Reddit('Temporary reddit app to obtain authentication information')

url = r.get_authorize_url(state='obtainingAuthentication', scope='modconfig modwiki wikiread privatemessages', refreshable=True)
webbrowser.open(url)
reddit_code = raw_input('Please enter the code from the redirect url: ')

access_information = r.get_access_information(reddit_code)

print 'Refresh token: ' + str(access_information.get('refresh_token'))
1 change: 0 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
username = "username"
password = "password"
subreddit = "subreddit"
Loading