Make OAuth compatible with Python 2

This commit is contained in:
Théo Piboubès
2015-08-28 20:28:58 +02:00
parent a2fa9c5ea3
commit d2822ccf85
2 changed files with 12 additions and 11 deletions

View File

@@ -79,7 +79,8 @@ def load_oauth_config():
defaults = dict(config.items('oauth')) defaults = dict(config.items('oauth'))
else: else:
# Populate OAuth section # Populate OAuth section
config['oauth'] = {'auto_login': False} config.add_section('oauth')
config.set('oauth', 'auto_login', 'false')
with open(get_config_fp(), 'w') as cfg: with open(get_config_fp(), 'w') as cfg:
config.write(cfg) config.write(cfg)
defaults = dict(config.items('oauth')) defaults = dict(config.items('oauth'))
@@ -154,7 +155,7 @@ def main():
reddit.config.decode_html_entities = False reddit.config.decode_html_entities = False
with curses_session() as stdscr: with curses_session() as stdscr:
oauth = OAuthTool(reddit, stdscr, LoadScreen(stdscr)) oauth = OAuthTool(reddit, stdscr, LoadScreen(stdscr))
if args.auto_login == 'True': # Ew! if args.auto_login == 'true': # Ew!
oauth.authorize() oauth.authorize()
if args.link: if args.link:
page = SubmissionPage(stdscr, reddit, oauth, url=args.link) page = SubmissionPage(stdscr, reddit, oauth, url=args.link)

View File

@@ -1,4 +1,3 @@
from six.moves import configparser
import curses import curses
import logging import logging
import os import os
@@ -7,6 +6,7 @@ import uuid
import webbrowser import webbrowser
import praw import praw
from six.moves import configparser
from . import config from . import config
from .curses_helpers import show_notification, prompt_input from .curses_helpers import show_notification, prompt_input
@@ -111,7 +111,7 @@ class OAuthTool(object):
try: try:
with self.loader(message='Refreshing token'): with self.loader(message='Refreshing token'):
new_access_info = self.reddit.refresh_access_information( new_access_info = self.reddit.refresh_access_information(
self.config['oauth']['refresh_token']) self.config.get('oauth', 'refresh_token'))
self.access_info = new_access_info self.access_info = new_access_info
self.reddit.set_access_credentials(scope=set(self.access_info['scope']), self.reddit.set_access_credentials(scope=set(self.access_info['scope']),
access_token=self.access_info['access_token'], access_token=self.access_info['access_token'],
@@ -121,8 +121,8 @@ class OAuthTool(object):
praw.errors.HTTPException) as e: praw.errors.HTTPException) as e:
show_notification(self.stdscr, ['Invalid OAuth data']) show_notification(self.stdscr, ['Invalid OAuth data'])
else: else:
self.config['oauth']['access_token'] = self.access_info['access_token'] self.config.set('oauth', 'access_token', self.access_info['access_token'])
self.config['oauth']['refresh_token'] = self.access_info['refresh_token'] self.config.set('oauth', 'refresh_token', self.access_info['refresh_token'])
self.save_config() self.save_config()
def authorize(self): def authorize(self):
@@ -132,7 +132,7 @@ class OAuthTool(object):
self.open_config(update=True) self.open_config(update=True)
# If no previous OAuth data found, starting from scratch # If no previous OAuth data found, starting from scratch
if 'oauth' not in self.config or 'access_token' not in self.config['oauth']: if not self.config.has_section('oauth') or not self.config.has_option('oauth', 'access_token'):
# Generate a random UUID # Generate a random UUID
hex_uuid = uuid.uuid4().hex hex_uuid = uuid.uuid4().hex
@@ -178,11 +178,11 @@ class OAuthTool(object):
except (praw.errors.OAuthAppRequired, praw.errors.OAuthInvalidToken) as e: except (praw.errors.OAuthAppRequired, praw.errors.OAuthInvalidToken) as e:
show_notification(self.stdscr, ['Invalid OAuth data']) show_notification(self.stdscr, ['Invalid OAuth data'])
else: else:
if 'oauth' not in self.config: if not self.config.has_section('oauth'):
self.config['oauth'] = {} self.config.add_section('oauth')
self.config['oauth']['access_token'] = self.access_info['access_token'] self.config.set('oauth', 'access_token', self.access_info['access_token'])
self.config['oauth']['refresh_token'] = self.access_info['refresh_token'] self.config.set('oauth', 'refresh_token', self.access_info['refresh_token'])
self.save_config() self.save_config()
# Otherwise, fetch new access token # Otherwise, fetch new access token
else: else: