[extractor/redgifs] Refresh auth token for 401 (#5352)

Closes #5351
Authored by: endotronic, pukkandan
pull/5464/head
Kevin Wood 2 years ago committed by GitHub
parent e14ea7fbd9
commit 8c188d5d09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,4 +1,5 @@
import functools import functools
import urllib
from .common import InfoExtractor from .common import InfoExtractor
from ..compat import compat_parse_qs from ..compat import compat_parse_qs
@ -72,14 +73,20 @@ class RedGifsBaseInfoExtractor(InfoExtractor):
self._API_HEADERS['authorization'] = f'Bearer {auth["token"]}' self._API_HEADERS['authorization'] = f'Bearer {auth["token"]}'
def _call_api(self, ep, video_id, *args, **kwargs): def _call_api(self, ep, video_id, *args, **kwargs):
if 'authorization' not in self._API_HEADERS: for attempt in range(2):
self._fetch_oauth_token(video_id) if 'authorization' not in self._API_HEADERS:
assert 'authorization' in self._API_HEADERS self._fetch_oauth_token(video_id)
try:
headers = dict(self._API_HEADERS) headers = dict(self._API_HEADERS)
headers['x-customheader'] = f'https://www.redgifs.com/watch/{video_id}' headers['x-customheader'] = f'https://www.redgifs.com/watch/{video_id}'
data = self._download_json( data = self._download_json(
f'https://api.redgifs.com/v2/{ep}', video_id, headers=headers, *args, **kwargs) f'https://api.redgifs.com/v2/{ep}', video_id, headers=headers, *args, **kwargs)
break
except ExtractorError as e:
if not attempt and isinstance(e.cause, urllib.error.HTTPError) and e.cause.code == 401:
del self._API_HEADERS['authorization'] # refresh the token
raise
if 'error' in data: if 'error' in data:
raise ExtractorError(f'RedGifs said: {data["error"]}', expected=True, video_id=video_id) raise ExtractorError(f'RedGifs said: {data["error"]}', expected=True, video_id=video_id)
return data return data

Loading…
Cancel
Save