Skip to content

Commit

Permalink
Merge pull request #331 from felipeucelli/oauth
Browse files Browse the repository at this point in the history
Fixed HTTP Error 400: Bad Request when using use_oauth
  • Loading branch information
JuanBindez authored Nov 16, 2024
2 parents f64de23 + 806e2ac commit eff3bf1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
30 changes: 25 additions & 5 deletions pytubefix/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(
WEB, WEB_EMBED, WEB_MUSIC, WEB_CREATOR, WEB_SAFARI,
ANDROID, ANDROID_MUSIC, ANDROID_CREATOR, ANDROID_VR, ANDROID_PRODUCER, ANDROID_TESTSUITE,
IOS, IOS_MUSIC, IOS_CREATOR,
MWEB, TV_EMBED, MEDIA_CONNECT.
MWEB, TV, TV_EMBED, MEDIA_CONNECT.
:param func on_progress_callback:
(Optional) User defined callback function for stream download
progress events.
Expand All @@ -99,6 +99,7 @@ def __init__(
It must be sent with the API along with the linked visitorData and
then passed as a `po_token` query parameter to affected clients.
If allow_oauth_cache is set to True, the user should only be prompted once.
(Do not use together with `use_oauth=True`)
:param Callable po_token_verifier:
(Optional) Verified used to obtain the visitorData and po_token.
The verifier will return the visitorData and po_token respectively.
Expand Down Expand Up @@ -135,7 +136,10 @@ def __init__(

self.client = 'WEB' if use_po_token else client

self.fallback_clients = ['WEB_EMBED', 'IOS', 'WEB']
# oauth can only be used by the TV and TV_EMBED client.
self.client = 'TV' if use_oauth else client

self.fallback_clients = ['MWEB', 'IOS', 'TV', 'WEB']

self._signature_timestamp: dict = {}

Expand Down Expand Up @@ -440,7 +444,7 @@ def vid_details(self):
return self._vid_details

innertube = InnerTube(
client='WEB',
client='TV' if self.use_oauth else 'WEB',
use_oauth=self.use_oauth,
allow_cache=self.allow_oauth_cache,
token_file=self.token_file,
Expand Down Expand Up @@ -675,12 +679,28 @@ def title(self) -> str:
"author", "unknown"
)


if self._title:
return self._title

try:
self._title = self.vid_info['videoDetails']['title']
# Some clients may not return the title in the `player` endpoint,
# so if it is not found we will look for it in the `next` endpoint
if 'title' in self.vid_info['videoDetails']:
self._title = self.vid_info['videoDetails']['title']
logger.debug('Found title in vid_info')
else:
logger.debug('Found title in vid_details')
self._title = self.vid_details['contents'][
'singleColumnWatchNextResults'][
'results'][
'results'][
'contents'][0][
'itemSectionRenderer'][
'contents'][0][
'videoMetadataRenderer'][
'title'][
'runs'][0][
'text']
except KeyError as e:
# Check_availability will raise the correct exception in most cases
# if it doesn't, ask for a report.
Expand Down
19 changes: 19 additions & 0 deletions pytubefix/innertube.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,25 @@
'require_po_token': False
},

'TV': {
'innertube_context': {
'context': {
'client': {
'clientName': 'TVHTML5',
'clientVersion': '7.20240813.07.00',
'platform': 'TV'
}
}
},
'header': {
'User-Agent': 'Mozilla/5.0',
'X-Youtube-Client-Name': '7'
},
'api_key': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8',
'require_js_player': True,
'require_po_token': False
},

'TV_EMBED': {
'innertube_context': {
'context': {
Expand Down

0 comments on commit eff3bf1

Please # to comment.