Make sure refresh token is always written to file
This commit is contained in:
parent
326eb484cc
commit
afe618916b
|
@ -78,7 +78,7 @@ class RedditDownloader:
|
||||||
self.cfg_parser.get('DEFAULT', 'client_secret'))
|
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, self.config_location)
|
||||||
|
|
||||||
self.authenticated = True
|
self.authenticated = True
|
||||||
self.reddit_instance = praw.Reddit(client_id=self.cfg_parser.get('DEFAULT', 'client_id'),
|
self.reddit_instance = praw.Reddit(client_id=self.cfg_parser.get('DEFAULT', 'client_id'),
|
||||||
|
|
|
@ -6,6 +6,7 @@ import logging
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import praw
|
import praw
|
||||||
import requests
|
import requests
|
||||||
|
@ -85,9 +86,10 @@ class OAuth2Authenticator:
|
||||||
|
|
||||||
|
|
||||||
class OAuth2TokenManager(praw.reddit.BaseTokenManager):
|
class OAuth2TokenManager(praw.reddit.BaseTokenManager):
|
||||||
def __init__(self, config: configparser.ConfigParser):
|
def __init__(self, config: configparser.ConfigParser, config_location: Path):
|
||||||
super(OAuth2TokenManager, self).__init__()
|
super(OAuth2TokenManager, self).__init__()
|
||||||
self.config = config
|
self.config = config
|
||||||
|
self.config_location = config_location
|
||||||
|
|
||||||
def pre_refresh_callback(self, authorizer: praw.reddit.Authorizer):
|
def pre_refresh_callback(self, authorizer: praw.reddit.Authorizer):
|
||||||
if authorizer.refresh_token is None:
|
if authorizer.refresh_token is None:
|
||||||
|
@ -98,3 +100,5 @@ class OAuth2TokenManager(praw.reddit.BaseTokenManager):
|
||||||
|
|
||||||
def post_refresh_callback(self, authorizer: praw.reddit.Authorizer):
|
def post_refresh_callback(self, authorizer: praw.reddit.Authorizer):
|
||||||
self.config.set('DEFAULT', 'user_token', authorizer.refresh_token)
|
self.config.set('DEFAULT', 'user_token', authorizer.refresh_token)
|
||||||
|
with open(self.config_location, 'w') as file:
|
||||||
|
self.config.write(file, True)
|
||||||
|
|
Loading…
Reference in a new issue