Fix OAuth2
This commit is contained in:
parent
36f516e3f0
commit
862121cac6
2 changed files with 13 additions and 4 deletions
|
@ -72,7 +72,10 @@ class RedditDownloader:
|
||||||
if not self.cfg_parser.has_option('DEFAULT', 'user_token'):
|
if not self.cfg_parser.has_option('DEFAULT', 'user_token'):
|
||||||
scopes = self.cfg_parser.get('DEFAULT', 'scopes')
|
scopes = self.cfg_parser.get('DEFAULT', 'scopes')
|
||||||
scopes = OAuth2Authenticator.split_scopes(scopes)
|
scopes = OAuth2Authenticator.split_scopes(scopes)
|
||||||
oauth2_authenticator = OAuth2Authenticator(scopes)
|
oauth2_authenticator = OAuth2Authenticator(
|
||||||
|
scopes,
|
||||||
|
self.cfg_parser.get('DEFAULT', 'client_id'),
|
||||||
|
self.cfg_parser.get('DEFAULT', 'client_secret'))
|
||||||
token = oauth2_authenticator.retrieve_new_token()
|
token = oauth2_authenticator.retrieve_new_token()
|
||||||
self.cfg_parser['DEFAULT']['user_token'] = token
|
self.cfg_parser['DEFAULT']['user_token'] = token
|
||||||
token_manager = OAuth2TokenManager(self.cfg_parser)
|
token_manager = OAuth2TokenManager(self.cfg_parser)
|
||||||
|
|
|
@ -17,9 +17,11 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class OAuth2Authenticator:
|
class OAuth2Authenticator:
|
||||||
|
|
||||||
def __init__(self, wanted_scopes: set[str]):
|
def __init__(self, wanted_scopes: set[str], client_id: str, client_secret: str):
|
||||||
self._check_scopes(wanted_scopes)
|
self._check_scopes(wanted_scopes)
|
||||||
self.scopes = wanted_scopes
|
self.scopes = wanted_scopes
|
||||||
|
self.client_id = client_id
|
||||||
|
self.client_secret = client_secret
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _check_scopes(wanted_scopes: set[str]):
|
def _check_scopes(wanted_scopes: set[str]):
|
||||||
|
@ -37,7 +39,11 @@ class OAuth2Authenticator:
|
||||||
return set(scopes)
|
return set(scopes)
|
||||||
|
|
||||||
def retrieve_new_token(self) -> str:
|
def retrieve_new_token(self) -> str:
|
||||||
reddit = praw.Reddit(redirect_uri='http://localhost:8080', user_agent='obtain_refresh_token for BDFR')
|
reddit = praw.Reddit(
|
||||||
|
redirect_uri='http://localhost:7634',
|
||||||
|
user_agent='obtain_refresh_token for BDFR',
|
||||||
|
client_id=self.client_id,
|
||||||
|
client_secret=self.client_secret)
|
||||||
state = str(random.randint(0, 65000))
|
state = str(random.randint(0, 65000))
|
||||||
url = reddit.auth.url(self.scopes, state, 'permanent')
|
url = reddit.auth.url(self.scopes, state, 'permanent')
|
||||||
logger.warning('Authentication action required before the program can proceed')
|
logger.warning('Authentication action required before the program can proceed')
|
||||||
|
@ -62,7 +68,7 @@ class OAuth2Authenticator:
|
||||||
def receive_connection() -> socket.socket:
|
def receive_connection() -> socket.socket:
|
||||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
server.bind(('localhost', 8080))
|
server.bind(('localhost', 7634))
|
||||||
logger.debug('Server listening on localhost:8080')
|
logger.debug('Server listening on localhost:8080')
|
||||||
|
|
||||||
server.listen(1)
|
server.listen(1)
|
||||||
|
|
Loading…
Reference in a new issue