0

I'm using spotipy to set spotify api credential on jupyter-lab. I have generated the spotify client secrect using client id and whenever i run this code it gives me the key error on client id and on client secrect key.

    sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=os.environ["SPOTIFY_CLIENT_ID"],client_secret=os.environ["SPOTIFY_CLIENT_SECRET"]))                     

PS: I have also imported "spotipy" and "spotipy.oauth2 import SpotifyClientCredentials" and they have been imported successfullly.

Below is the code and the error snippet: code

error

2 Answers2

0

These are environmental variables. You need to set them in your environment before running that code. You can set these within your Jupyter notebook with the magic commands. Copying the below from [michael's answer here][1]:

To set an env variable in a jupyter notebook, just use a % magic 
commands, either %env or %set_env, e.g., %env MY_VAR=MY_VALUE or 
%env MY_VAR MY_VALUE. (Use %env by itself to print out current 
environmental variables.)

[1]: https://stackoverflow.com/questions/37890898/how-to-set-env-variable-in-jupyter-notebook#:~:text=To%20set%20an%20env%20variable,print%20out%20current%20environmental%20variables.)

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
  • Thanks for the answer, I have tried setting the variable using %set_env and it executed perfectly but i am still getting the same 'key error' on client id when i run the above code in the question. can you elaborate to me a little more or give me something else to try ? thanks – Haroon khan Aug 15 '22 at 14:50
0

So I have went the other way on my jupyter-lab and it worked.

I did the following in order to achieve it:

Create a token first with this line of code:

token = SpotifyClientCredentials(client_id="client_id", client_secret="client_secret").get_access_token()

and pass the token to spotipy like this:

sp = spotipy.Spotify(token)

use the 'sp' variable in your later code and it works. No 'key errors' were thrown at the execution of above code, Thanks :)