1
0
Fork 0
mirror of synced 2024-06-17 09:44:39 +12:00

Make sure refresh token is always written to file

This commit is contained in:
Serene-Arc 2021-03-09 19:20:15 +10:00 committed by Ali Parlakci
parent 326eb484cc
commit afe618916b
2 changed files with 6 additions and 2 deletions

View file

@ -78,7 +78,7 @@ class RedditDownloader:
self.cfg_parser.get('DEFAULT', 'client_secret'))
token = oauth2_authenticator.retrieve_new_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.reddit_instance = praw.Reddit(client_id=self.cfg_parser.get('DEFAULT', 'client_id'),

View file

@ -6,6 +6,7 @@ import logging
import random
import re
import socket
from pathlib import Path
import praw
import requests
@ -85,9 +86,10 @@ class OAuth2Authenticator:
class OAuth2TokenManager(praw.reddit.BaseTokenManager):
def __init__(self, config: configparser.ConfigParser):
def __init__(self, config: configparser.ConfigParser, config_location: Path):
super(OAuth2TokenManager, self).__init__()
self.config = config
self.config_location = config_location
def pre_refresh_callback(self, authorizer: praw.reddit.Authorizer):
if authorizer.refresh_token is None:
@ -98,3 +100,5 @@ class OAuth2TokenManager(praw.reddit.BaseTokenManager):
def post_refresh_callback(self, authorizer: praw.reddit.Authorizer):
self.config.set('DEFAULT', 'user_token', authorizer.refresh_token)
with open(self.config_location, 'w') as file:
self.config.write(file, True)