[vevo] rewrite authentication
- token is now sent in `Authorization` header - token is refreshed when needed - new token is not requested if there is one (playlist downloading)
This commit is contained in:
parent
7906460606
commit
60de4c4410
@ -153,41 +153,58 @@ class VevoIE(VevoBaseIE):
|
|||||||
4: 'amazon',
|
4: 'amazon',
|
||||||
}
|
}
|
||||||
|
|
||||||
def _initialize_api(self, video_id):
|
def _initialize_api(self, refresh=False):
|
||||||
webpage = self._download_webpage(
|
data = {'client_id': 'SPupX1tvqFEopQ1YS6SS'}
|
||||||
'https://accounts.vevo.com/token', None,
|
if refresh:
|
||||||
note='Retrieving oauth token',
|
data.update({
|
||||||
errnote='Unable to retrieve oauth token',
|
'grant_type': 'refresh_token',
|
||||||
data=json.dumps({
|
'refresh_token': self._REFRESH_TOKEN,
|
||||||
'client_id': 'SPupX1tvqFEopQ1YS6SS',
|
|
||||||
'grant_type': 'urn:vevo:params:oauth:grant-type:anonymous',
|
|
||||||
}).encode('utf-8'),
|
|
||||||
headers={
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
})
|
})
|
||||||
|
else:
|
||||||
|
data['grant_type'] = 'urn:vevo:params:oauth:grant-type:anonymous'
|
||||||
|
post_data = json.dumps(data).encode('utf-8')
|
||||||
|
|
||||||
|
webpage = self._download_webpage(
|
||||||
|
'https://accounts.vevo.com/token', 'token',
|
||||||
|
data=post_data, headers={'Content-Type': 'application/json'},
|
||||||
|
note='Retrieving oauth token',
|
||||||
|
errnote='Unable to retrieve oauth token')
|
||||||
|
|
||||||
if re.search(r'(?i)THIS PAGE IS CURRENTLY UNAVAILABLE IN YOUR REGION', webpage):
|
if re.search(r'(?i)THIS PAGE IS CURRENTLY UNAVAILABLE IN YOUR REGION', webpage):
|
||||||
self.raise_geo_restricted(
|
self.raise_geo_restricted(
|
||||||
'%s said: This page is currently unavailable in your region' % self.IE_NAME)
|
'%s said: This page is currently unavailable in your region' % self.IE_NAME)
|
||||||
|
|
||||||
auth_info = self._parse_json(webpage, video_id)
|
auth_info = self._parse_json(webpage, 'token')
|
||||||
self._api_url_template = self.http_scheme() + '//apiv2.vevo.com/%s?token=' + auth_info['legacy_token']
|
|
||||||
|
self._ACCESS_TOKEN = auth_info['legacy_token']
|
||||||
|
self._REFRESH_TOKEN = auth_info['refresh_token']
|
||||||
|
|
||||||
def _call_api(self, path, *args, **kwargs):
|
def _call_api(self, path, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
data = self._download_json(self._api_url_template % path, *args, **kwargs)
|
data = self._download_json(
|
||||||
|
'https://apiv2.vevo.com/%s' % path, headers={
|
||||||
|
'Authorization': 'Bearer %s' % self._ACCESS_TOKEN,
|
||||||
|
}, *args, **kwargs)
|
||||||
except ExtractorError as e:
|
except ExtractorError as e:
|
||||||
if isinstance(e.cause, compat_HTTPError):
|
if isinstance(e.cause, compat_HTTPError):
|
||||||
errors = self._parse_json(e.cause.read().decode(), None)['errors']
|
errors = self._parse_json(e.cause.read().decode(), None)['errors']
|
||||||
error_message = ', '.join([error['message'] for error in errors])
|
error_messages = [error['message'] for error in errors]
|
||||||
raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
|
if 'Token is expired' in error_messages:
|
||||||
|
self._initialize_api(refresh=True)
|
||||||
|
return self._call_api(path, *args, **kwargs)
|
||||||
|
else:
|
||||||
|
error_message = ', '.join(error_messages)
|
||||||
|
raise ExtractorError(
|
||||||
|
'%s said: %s' % (self.IE_NAME, error_message),
|
||||||
|
expected=True)
|
||||||
raise
|
raise
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def _real_extract(self, url):
|
def _real_extract(self, url):
|
||||||
video_id = self._match_id(url)
|
video_id = self._match_id(url)
|
||||||
|
|
||||||
self._initialize_api(video_id)
|
if not hasattr(self, '_ACCESS_TOKEN'):
|
||||||
|
self._initialize_api()
|
||||||
|
|
||||||
video_info = self._call_api(
|
video_info = self._call_api(
|
||||||
'video/%s' % video_id, video_id, 'Downloading api video info',
|
'video/%s' % video_id, video_id, 'Downloading api video info',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user