alessandrofajr / vigia-youtube

O Vigia do YouTube pode monitorar canais na plataforma de vídeos do Google e disparar um e-mail caso um material tenha sido removido, ocultado ou apagado.
11 stars 0 forks source link

raise KeyError(key) from None #1

Open 3d0n1 opened 2 years ago

3d0n1 commented 2 years ago

Hello when i try to run this script i got this error

C:\Users\thexc\AppData\Local\Programs\Python\Python39\vigiayt>youtube_api_scraper.py Traceback (most recent call last): File "C:\Users\thexc\AppData\Local\Programs\Python\Python39\vigiayt\youtube_api_scraper.py", line 13, in <module> youTubeApiKey = os.environ["AIzaSyBR3v_ZKK5LF6*****************"] File "C:\Users\thexc\AppData\Local\Programs\Python\Python39\lib\os.py", line 679, in __getitem__ raise KeyError(key) from None KeyError: 'AIzaSyBR3v_ZKK5LF6*******************'

my api key working fine but there is something with i think os.environ what can it be? thanks.

alessandrofajr commented 2 years ago

Hi! The os module is needed to call a variable that is in another file in my system (in this case, to not reveal my credentials). You shouldn't put your API key inside the os.environ. Try to put as a plain string after the variable youTubeApiKey (you should do the same for the other variables that uses os.environ)

But only use as string if you are running the code locally, to avoid exposing your credentials.

I recommend watching this video for more information about os module: https://www.youtube.com/watch?v=IolxqkL7cD8

Let me know if it works

3d0n1 commented 2 years ago

Hello, thank you for trying to help me but it didn't help me because I'm a complete beginner at Python.

`youTubeApiKey = 'AIzaSyBR3v*****', os.environ["YOUTUBE_API_KEY"] youtube = build('youtube', 'v3', developerKey=youTubeApiKey) #Or should i add it here?

i also try with environment variable and with os.environ.get etc but I had no success expect one "i think" explanation below

For something which I tried seemed that I succeeded with ytapi and channel id but I came to sheets decode base 64 error so I went back to the beginning.

please understand that I am a beginner and have no idea what I am doing forgive me for that :)

cuducos commented 2 years ago

Hi @3d0n1, I understand you're just getting starting with Python, and that's totally fine — hope you're enjoying it : )

The error you are facing seems to be two-folded:

Python's os.environ

In Python, the os.environ is used to read environment variables set outside Python (usually through the terminal or operational system, for example). To keep it simple, os.environ is like a Python dictionary, so if you have something like os.environ["YOUTUBE_API_KEY"] it accesses an environment variable called YOUTUBE_API_KEY.

When I see that your error contains a line like:

os.environ["AIzaSyBR3v_ZKK5LF6*****************"]

I assume two things:

Usage of environment variables

I am of little help here because I know nothing about Windows (and, from your logs, you're using Windows). But you can search for something like how to create an environmental variable in Windows and probably go through it. The idea is to create an environment variable called YOUTUBE_API_KEY which holds the value that is your API key, e.g. AIzaSyBR3v_ZKK5LF6*****************. In Linux (using Bash or Zsh), for example, I can do that by typing this command in the terminal:

$ export YOUTUBE_API_KEY=AIzaSyBR3v_ZKK5LF6*****************

And then running the script from this same terminal session.

TLDR

You are trying to hardcode the API key in the source code which is not how this repository was designed. Your challenge is not Python, but finding out how to set up an environment variable in your OS of choice : )

Does that make sense?

3d0n1 commented 2 years ago

Hello @cuducos

That's what i said above i tried yesterday with environment variable and yes I had success but it only works with youtube api but not with GOOGLE_SHEETS_CREDENTIALS in which case it only gives an error

raise KeyError(key) from None KeyError: 'GOOGLE_SHEETS_CREDENTIALS'

i'm pretty sure i added sheet credentials in environment variable 'windows 10' i tried also directly in python "even though you guys recommended it's not good practice" it didn't work either.

But it doesn't matter so much now i'm a beginner with a few days experience and i'm trying to learn directly from complete tools.

Yes i enjoy python it has so much interesting things and it's fun even when it's hard :) thanks for the help i learned a lot :)

alessandrofajr commented 1 year ago

Hello, @3d0n1! Sorry for the very, very delayed response. I hope you're still interested in solving this problem. I think I found the reason for the error...

When you have the GOOGLE_SHEETS_CREDENTIALS ready to be set up in your environment variable, it's important that it is encoded in Base64. This is because these credentials in the gspread library are read from a dictionary/JSON... And since we can't set up a dictionary as an environment variable, we encode it to Base64 and then pass the parameter in the line service_account = gspread.service_account_from_dict(credentials).

If you need help on how to obtain these Service Account credentials, the documentation of the gspread library will assist you: https://docs.gspread.org/en/latest/oauth2.html

After obtaining the JSON with your credentials for Google Sheets, encode its content in Base64 and then set the GOOGLE_SHEETS_CREDENTIALS environment variable.

Once again, sorry for the late response. I hope this helps you.