def _prepare_common_authorizer(self, authenticator): if self._token_manager is not None: warn( "Token managers have been depreciated and will be removed in the near" " future. See https://www.reddit.com/r/redditdev/comments/olk5e6/" "followup_oauth2_api_changes_regarding_refresh/ for more details.", category=DeprecationWarning, stacklevel=2, ) if self.config.refresh_token: raise TypeError( "``refresh_token`` setting cannot be provided when providing" " ``token_manager``") self._token_manager.reddit = self authorizer = Authorizer( authenticator, post_refresh_callback=self._token_manager. post_refresh_callback, pre_refresh_callback=self._token_manager.pre_refresh_callback, ) elif self.config.refresh_token: authorizer = Authorizer(authenticator, refresh_token=self.config.refresh_token) else: self._core = self._read_only_core return self._core = self._authorized_core = session(authorizer)
def authorize(self, code): """Complete the web authorization flow and return the refresh token. :param code: The code obtained through the request to the redirect uri. :returns: The obtained refresh token, if available, otherwise ``None``. The session's active authorization will be updated upon success. """ authenticator = self._reddit._read_only_core._authorizer._authenticator authorizer = Authorizer(authenticator) authorizer.authorize(code) authorized_session = session(authorizer) self._reddit._core = self._reddit._authorized_core = authorized_session return authorizer.refresh_token
def authorize(self, code): """Complete the web authorization flow and return the refresh token. :param code: The code obtained through the request to the redirect uri. :returns: The obtained refresh token, if available, otherwise ``None``. The session's active authorization will be updated upon success. """ authenticator = self._reddit._read_only_core._authorizer._authenticator authorizer = Authorizer(authenticator) authorizer.authorize(code) authorized_session = session(authorizer) self._reddit._core = self._reddit._authorized_core = authorized_session return authorizer.refresh_token
def authorize(self, code): """Complete the web authorization flow and return the refresh token. :param code: The code obtained through the request to the redirect uri. :returns: The obtained refresh token, if available, otherwise ``None``. The session's active authorization will be updated upon success. """ authenticator = self._reddit._read_only_core._authorizer._authenticator if not isinstance(authenticator, TrustedAuthenticator) or \ self._reddit.config.username: raise ClientException('authorize can only be used with web apps.') authorizer = Authorizer(authenticator) authorizer.authorize(code) authorized_session = session(authorizer) self._reddit._core = self._reddit._authorized_core = authorized_session return authorizer.refresh_token
def _prepare_untrusted_prawcore(self, requestor): authenticator = UntrustedAuthenticator(requestor, self.config.client_id, self.config.redirect_uri) read_only_authorizer = DeviceIDAuthorizer(authenticator) self._read_only_core = session(read_only_authorizer) if self.config.refresh_token: authorizer = Authorizer(authenticator, self.config.refresh_token) self._core = self._authorized_core = session(authorizer) else: self._core = self._read_only_core
def _prepare_common_authorizer(self, authenticator): if self._token_manager is not None: if self.config._do_not_use_refresh_token != self.config.CONFIG_NOT_SET: raise TypeError( "legacy ``refresh_token`` setting cannot be provided when providing" " ``token_manager``") self._token_manager.reddit = self authorizer = Authorizer( authenticator, post_refresh_callback=self._token_manager. post_refresh_callback, pre_refresh_callback=self._token_manager.pre_refresh_callback, ) elif self.config._do_not_use_refresh_token != self.config.CONFIG_NOT_SET: authorizer = Authorizer( authenticator, refresh_token=self.config._do_not_use_refresh_token) else: self._core = self._read_only_core return self._core = self._authorized_core = session(authorizer)
def _prepare_trusted_prawcore(self, requestor): authenticator = TrustedAuthenticator(requestor, self.config.client_id, self.config.client_secret, self.config.redirect_uri) read_only_authorizer = ReadOnlyAuthorizer(authenticator) self._read_only_core = session(read_only_authorizer) if self.config.username and self.config.password: script_authorizer = ScriptAuthorizer( authenticator, self.config.username, self.config.password) self._core = self._authorized_core = session(script_authorizer) elif self.config.refresh_token: authorizer = Authorizer(authenticator, self.config.refresh_token) self._core = self._authorized_core = session(authorizer) else: self._core = self._read_only_core
def __init__(self, **kwargs): log_level = kwargs.pop('log_level', logging.NOTSET) logging.basicConfig(level=log_level, filename='/dev/null', format='%(asctime)s %(levelname)s %(message)s', datefmt="%Y-%m-%d %H:%M:%S") log_prefix = kwargs.pop('log_prefix', None) if log_prefix: ts = datetime.datetime.fromtimestamp( time()).strftime('%Y%m%d.%H%M%S') logging.getLogger().addHandler(logging.FileHandler(log_prefix + ts)) localhost = kwargs.pop('localhost', '127.0.0.1') default_kwargs = { 'history_file': rtv.config.HISTORY, 'token_file': rtv.config.TOKEN, 'redirect_uri': 'http://' + localhost + ':17973', 'client_id': 'KBV2seGZgHOa9g', 'client_secret': 'cannot-be-empty', 'redirect_port': 17973, 'user_agent': praw.const.USER_AGENT_FORMAT.\ format(':'.join([os.uname()[0], 'nnreddit', __version__])), } default_kwargs = { k: v for k, v in default_kwargs.items() if k not in kwargs } kwargs.update(default_kwargs) cfg = rtv.config.Config(**kwargs) cfg.load_history() cfg.load_refresh_token() cfg.config['refresh_token'] = cfg.refresh_token logging.getLogger().debug("Refresh token: %s", cfg.token_file) super(AuthenticatedReddit, self).__init__(**cfg.config) if not cfg.refresh_token: self._core \ = self._authorized_core \ = session(Authorizer(self._core._authorizer._authenticator)) state = str(random.randint(0, 65000)) url = self._authorized_core._authorizer._authenticator.\ authorize_url('permanent', ['edit', 'history', 'identity', 'mysubreddits', 'privatemessages', 'read', 'report', 'save', 'submit', 'subscribe', 'vote'], state) docs_sub = re.compile(r'reddit terminal viewer', re.IGNORECASE) docs.OAUTH_SUCCESS = docs_sub.sub('nnreddit', docs.OAUTH_SUCCESS) docs.OAUTH_ACCESS_DENIED = docs_sub.sub('nnreddit', docs.OAUTH_ACCESS_DENIED) print("::user::Please check your browser.", file=sys.stderr) if cfg.token_file == "/dev/null": cfg.refresh_token = None else: p = Process(target=self.open_url_silent, args=(url, )) p.start() try: p.join(7) if p.is_alive(): raise BrowserError( 'Timeout waiting for browser to open') finally: try: p.terminate() except OSError: pass server = OAuthHTTPServer(('', cfg.config['redirect_port']), OAuthHandler) server.serve_forever() self._authorized_core._authorizer.authorize( OAuthHandler.params['code']) cfg.refresh_token = self._authorized_core._authorizer.refresh_token cfg.save_refresh_token() if 'history_size' in cfg.config: cfg.save_history() self._bodies = {} self._stream_comm = {} self._stream_subm = {}