Suboda
Suboda

Reputation: 1

Pytubefix Issue: Handling ‘Login Required’ on AWS EC2 Deployed Applications

I was running an application that requires the PytubeFix library to extract content from YouTube videos. It runs locally without any problems, but when I deploy it, it doesn’t work. On the server, it logs a message saying “Login required” with the video ID, as shown in the image.

OS : ubuntu 22.04
Python : 3.10
pytubefix : 6.6.2

In pytubefix, we can define use_oauth=True as shown below:

from pytubefix import YouTube

video = Youtube(url, use_oauth=True)

#rest of the code

When applying this, it prompts a message to authenticate with OAuth via a given URL and key. After authentication, we have to press Enter on the terminal. This worked fine in the local environment.

However, after deployed to EC2, we cannot perform this action

Issue - https://github.com/JuanBindez/pytubefix/issues/119

I have successfully come up with a solution as outlined in the steps below

  1. Use the local environment to authenticate with PytubeFix’s required authentication (use the given URL to log into your Google account and press Enter on the terminal). Don't use your main google account
  2. Find the cached authentication credentials inside the PytubeFix library. Most likely, it will be located at venv/lib/python3.10/site-packages/pytubefix/__cache__/tokens.json
  3. Open your AWS console, go to EC2 instances, and connect to the EC2 instance via the AWS terminal
  4. Locate your project files and navigate to the venv/lib/python3.10/site-packages/pytubefix directory within your project.
  5. If it doesn’t exist, create a directory named "__cache__" (mkdir __cache__) inside the pytubefix directory and create a file called "tokens.json" (touch tokens.json) inside the __cache__ directory
  6. Copy the contents of your local "tokens.json" file and paste them into the EC2 "tokens.json" file. Use nano tokens.json to open the file, paste the code, save it, and exit
  7. Restart the server via the AWS console or using supervisorctl

Now the issue is fixed, and videos are being extracted from YouTube without the “Login required” or “Private video” issues

Issue: You need to update the tokens.json file when the OAuth token expires

While this solution works for now, it is not consistent for long-term use. Is there a more sustainable solution for using this library over an extended period?

Upvotes: 0

Views: 596

Answers (0)

Related Questions