-2

can someone tell me what's wrong with my code? I am building a voice assistant for Spotify. I have made two .py files, one holds all the 'get uri' and 'play track' functions, and the other is used to authorize access using the client ID, Client Secret, and scope fetched from the Spotifys' developer forum by creating an app. Moreover, I have provided my PC's name as the device name and, the redirect uris. When running the main file, it is throwing a KeyError with context to the client ID. Here's the code:

import pandas as pd
from speech_recognition import Microphone, Recognizer, UnknownValueError
import spotipy as sp
from spotipy.oauth2 import SpotifyOAuth

from Spots import spotsFunctions as sf, InvalidSearchError


# Set variables from setup.txt
setup = pd.read_csv('userDetails.txt', sep='=', index_col=0, header=None).squeeze()
clientID = setup['clientID']
clientSecret = setup['clientSecret']
deviceName = setup['deviceName']
redirectUri = setup['redirectUri']
scope = setup['scope']
username = setup['username']

# Connecting to the Spotify account
auth_manager = SpotifyOAuth(
    client_id = clientID,
    client_secret=clientSecret,
    redirect_uri=redirectUri,
    scope=scope,
    username=username)
spotify = sp.Spotify(auth_manager=auth_manager)

And the error it's throwing:

File "D:\Spotify Voice Assistant\venv\lib\site-packages\pandas\core\indexes\base.py", line 3800, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 165, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'clientID'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\Spotify Voice Assistant\main.py", line 11, in <module>
    clientID = setup['clientID']
  File "D:\Spotify Voice Assistant\venv\lib\site-packages\pandas\core\series.py", line 982, in __getitem__
    return self._get_value(key)
  File "D:\Spotify Voice Assistant\venv\lib\site-packages\pandas\core\series.py", line 1092, in _get_value
    loc = self.index.get_loc(label)
  File "D:\Spotify Voice Assistant\venv\lib\site-packages\pandas\core\indexes\base.py", line 3802, in get_loc
    raise KeyError(key) from err
KeyError: 'clientID'
buran
  • 13,682
  • 10
  • 36
  • 61
Taha Habib
  • 21
  • 4
  • 2
    It's clear in the error that there's no `clientID` in `setup`. It's hard to tell without seeing the contents of `userDetails.txt` but by looking at the naming convention, could it be `clientId`? – Harun Yilmaz Oct 03 '22 at 13:44
  • 1
    Also, I doubt it will work, after you fix the `KeyError` given that you read the txt file into pandas dataframe and then pass `Series` objects when instantiate `SpotifyOAuth`. I would guess the setup txt file is not in format to be read with pandas in the first place – buran Oct 03 '22 at 13:45
  • Show how your `userDetails.txt` file looks like (replace sensitive information with dummy data) – buran Oct 03 '22 at 13:58
  • clientID = 72*********************13 clientSecret = 4f**************************2a deviceName = DESKTOP-4RFLLTU redirectUri = https://example.com/callback/ username = 2jxmwb1nh7oqdzdnjplkd1d scope = playlist-read-private user-modify-playback-state user-read-currently-playing user-read-playback-state – Taha Habib Oct 03 '22 at 14:08
  • Please, edit your question to include the relevant information (properly formatted). It looks like you want to use `configparser`, not `pandas` – buran Oct 03 '22 at 16:31
  • check https://stackoverflow.com/questions/19379120/how-to-read-a-config-file-using-python and https://docs.python.org/3/library/configparser.html – buran Oct 03 '22 at 16:38

1 Answers1

0

The line

clientID = setup['clientID']

Requires the dictionary setup to have a key clientID.

But it seems the dictionary doesn't have it:

KeyError: 'clientID'

Since the dictionary is created using the file userDetails.txt, it is probably a problem with the structure or the contents of that file.

You could check if the file contains an entry for clientID.

Hope it helps!

ezekiel
  • 9
  • 2