JuanBindez/pytubefix

c0gBbiUZe6k requires login to view

Closed this issue · 20 comments

i have this error
c0gBbiUZe6k requires login to view
what to do now?

It still happening in 6.9.2

2024-08-07T02:13:53.803Z | Retry failed �[91mozTWlCoI5BE requires login to view�[0mTraceback (most recent call last):
-- | --
  | 2024-08-07T02:13:53.803Z | File "/var/task/pytubefix/__main__.py", line 612, in title
  | 2024-08-07T02:13:53.803Z | self._title = self.vid_info['videoDetails']['title']
  | 2024-08-07T02:13:53.803Z | KeyError: 'videoDetails'
  | 2024-08-07T02:13:53.803Z | During handling of the above exception, another exception occurred:
  | 2024-08-07T02:13:53.803Z | Traceback (most recent call last):
  | 2024-08-07T02:13:53.803Z | File "/var/task/handler.py", line 577, in entry
  | 2024-08-07T02:13:53.803Z | title, video_length, description = get_title(
  | 2024-08-07T02:13:53.803Z | File "/var/task/utils/misc.py", line 15, in wrapper
  | 2024-08-07T02:13:53.803Z | return func(*args, **kwargs)
  | 2024-08-07T02:13:53.803Z | File "/var/task/handler.py", line 104, in get_title
  | 2024-08-07T02:13:53.803Z | return yt.title, yt.length, yt.description
  | 2024-08-07T02:13:53.803Z | File "/var/task/pytubefix/__main__.py", line 616, in title

I am facing similar issue

Similar issue

Same problem

same problem

yt = YouTube(url, use_oauth=True, allow_oauth_cache=True, on_progress_callback = on_progress)

Good day.

I'm also getting this error as the others but the actual error coming from Youtube/Google is

{'responseContext': {'visitorData': 'Cgs4QkpLcTVJOS1OcyjVis61BjIiCgJCRxIcEhgSFgsMDg8QERITFBUWFxgZGhscHR4fICEgXQ%3D%3D', 'maxAgeSeconds': 0}, 'playabilityStatus': {'status': 'LOGIN_REQUIRED', 'reason': 'Sign in to confirm you’re not a bot'}, 'trackingParams': 'CAAQu2kiEwic-IiqjOOHAxWy6UIFHawtCfA=', 'adBreakHeartbeatParams': 'Q0FBJTNE'}

This happens with youtubefix search class

Regards

what code are you using to test?

I'm using s = Search(query)
then call s.videos
loop over this then call
s.get_next_results()

when all is done and i'm getting video.title from each Youtube object and this when the error happens.

Regards

Testing now seems fine/ok

the same problem
File "/usr/local/lib/python3.10/site-packages/pytubefix/main.py", line 616, in title
self.check_availability()
File "/usr/local/lib/python3.10/site-packages/pytubefix/main.py", line 310, in check_availability
raise exceptions.LoginRequired(video_id=self.video_id)
pytubefix.exceptions.LoginRequired: M7-h3FO-KKo requires login to view

yt = YouTube(url, use_oauth=True, allow_oauth_cache=True, on_progress_callback = on_progress)

Yes! it works out on me. It requires you to connect Youtube with Youe google account ,and the problem "requires login to view" will be fixed

Problem here is when using Search() class the YouTube() class is called internally to create the YouTube objects so then would need to update pytubefix/contrib/search.py module under
def fetch_and_parse(self, continuation=None):
# Get videos results
if 'videoRenderer' in video_details:
videos.append(YouTube(f"https://www.youtube.com/watch?v={video_details['videoRenderer']['videoId']}"))

to use_oauth=True, allow_oauth_cache=True

yt = YouTube(url, use_oauth=True, allow_oauth_cache=True, on_progress_callback = on_progress)

Is this the solution? What was the cause of the issue and how does this solution solve it?
FYI I am running my code on AWS Lambda function

I did try the oauth but haven't tested enough to see if the if I still get login errors.

So this thing with YouTube login to make sure you are not a bot is newish because after writing my app code it's only now in last few days that I got this issue.

After using oauth once then I do need to remove or set oauth back to false otherwise it keeps asking to login with the code.

Now I just need to come up with a more elegant way to handle the oauth prompt in pyside6 app.

I have saame problem but i can ot auth because i use pytube with docker

This issue is still happening even when oauth was used.

{'responseContext': {'visitorData': 'Cgsxc0hqRmVodmJrMCjOnte1BjIiCgJCRxIcEhgSFgsMDg8QERITFBUWFxgZGhscHR4fICEgGw%3D%3D', 'maxAgeSeconds': 0}, 'playabilityStatus': {'status': 'LOGIN_REQUIRED', 'reason': 'Sign in to confirm you’re not a bot'}, 'trackingParams': 'CAAQu2kiEwjH8OmTu-eHAxUy5kIFHVziC_A=', 'adBreakHeartbeatParams': 'Q0FBJTNE'}

Also following the innertube code when oauth is set to True it should be creating/reading from a tokens file which i see its not doing because its always asking to login with a code even though i have already done this. Also i cant find the file tokens.json file anywhere.

Ok so the problem i face is im using the pytubefix search class to return n amount of search result from youtube using a generator to yield the results like so

def search_generator(self, query: str, max_result: int):
search_results: Search = Search(query)
displayed_video_ids: Set[str] = set()
total_videos_fetched = 0

    while True:
        # Iterate over new videos
        for video in search_results.videos:
            if video.video_id in displayed_video_ids:
                continue
            
            yield video

            # Update the set of displayed video IDs
            displayed_video_ids.add(video.video_id)

            # Update the total number of videos fetched
            total_videos_fetched += 1

            # Check if we have enough results
            if total_videos_fetched == max_result:
                return
            
        try:
            search_results.get_next_results()
        except Exception:
            return

lets say i set the max_result = 50
Currently i need to update search.py module to include use_oauth=True otherwise i get the login_required error after awhile.

Get videos results

            if 'videoRenderer' in video_details:
                videos.append(YouTube(f"https://www.youtube.com/watch?v="
                                      f"{video_details['videoRenderer']['videoId']}", use_oauth=True))

def fetch_bearer_token(self):
print(f'Please open {verification_url} and input code {user_code}')
input('Press enter when you have completed this step.')

with 50 results i get this 50 x
print(f'Please open {verification_url} and input code {user_code}')
and this does not work or apply anymore - input('Press enter when you have completed this step.')
so that's why the tokens.json file was never created.

If i set my max_result to 1 then i can do this
input('Press enter when you have completed this step.') - after using the code and the tokens file is created.

I think you may need to allow to pass the use_oauth=True argument in the Search class

class Search:
def init(self, query):
"""Initialize Search object.

    :param str query:
        Search query provided by the user.
    """
    self.query = query
    self._innertube_client = InnerTube(client='WEB')

    # The first search, without a continuation, is structured differently
    #  and contains completion suggestions, so we must store this separately
    self._initial_results = None

    self._results = {}
    self._completion_suggestions = None

    # Used for keeping track of query continuations so that new results
    #  are always returned when get_next_results() is called
    self._current_continuation = None

Same problem, haven't found a solution yet, I am also running it in a docker container in AWS Lambda....

for those running this on lambda, I got it working by getting token data from google oauth playground

  1. Go to the Google OAuth 2.0 Playground (https://developers.google.com/oauthplayground/)
  2. On the left side, scroll down and select "YouTube Data API v3" under the YouTube APIs section.
  3. Select the scopes you need, "https://www.googleapis.com/auth/youtube.readonly" for read-only access.
  4. Click the "Authorize APIs" button.

then create the token file before you instantiate YouTube

    data = {
        "access_token": os.getenv("access_token"),
        "refresh_token": os.getenv("refresh_token"),
        "expires": 1823266077 # some number in the future so it doesn't try refreshing the bearer token
    }
    with open("/tmp/token.json", "w") as f: # make sure you write to /tmp for lambda
        json.dump(data, f)

then specify the token_file parameter for YouTube

yt = YouTube(
        url, use_oauth=True, allow_oauth_cache=True, token_file="/tmp/token.json"
)

take a look at #170

token_file

Tried out this one and got HTTP Error 401: Unauthorized