spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
4.97k stars 956 forks source link

An error occurred: can only concatenate tuple (not "str") to tuple when using Spotipy functions #1151

Closed olistori closed 1 month ago

olistori commented 1 month ago

Think there is a problem with how spotipy is handling strings in some functions. Atleast .track() and .playlist_tracks() here is a code snipped im using, query is a link to a spotify playlist.

        playlist_id = query.split("/")[-1].split("?")[0]
        print(playlist_id)
        try:
            playlist = spotify.playlist_tracks(playlist_id)
        except Exception as e:
            print(f'Error while getting playlist data: {str(e)}')
            logging.error(f'Error while getting playlist data: {str(e)}')

ive tried using URL, URI and Playlist/track id

Output An error occurred: can only concatenate tuple (not "str") to tuple

Environment:

stephanebruckert commented 1 month ago

What does playlist_id print?

olistori commented 1 month ago

3ntvwrQ3TSHo2k3wFjN62s i guess its the id for the playlist

on .track ive tried the url, id and ur:

        try:
            track = spotify.track(query)
        except Exception as e:
            print(f"An error occurred: {e}")
            logging.error(f"An error occurred: {e}")
stephanebruckert commented 1 month ago

Okay strange, could you please share the full stack trace you are seeing?

olistori commented 1 month ago

When i put logging level in DEBUG, this is all i get when i try to call the functions:

DEBUG:spotipy.cache_handler:cache does not exist at: .cache DEBUG:spotipy.cache_handler:cache does not exist at: .cache ERROR:root:An error occurred: can only concatenate tuple (not "str") to tuple

stephanebruckert commented 1 month ago

The stack trace shows the exact line where there is an issue, could you share that? It's basically the full error you are seeing, please copy paste everything you see

Edit: I just saw you are swallowing the error. To see the full error in here you need to either 1) remove the whole try/except

    track = spotify.track(query)

or 2) throw the error again:

    try:
        track = spotify.track(query)
    except Exception as e:
        print(f"An error occurred: {e}")
        logging.error(f"An error occurred: {e}")
        throw(e)

or 3) print the stack trace from Except https://stackoverflow.com/a/16946886/1515819:

    import traceback

    try:
        track = spotify.track(query)
    except Exception as e:
        print(f"An error occurred: {e}")
        logging.error(f"An error occurred: {e}")
        traceback.print_exc()
olistori commented 1 month ago

Ahh i see, the error is not with the function call. Looks like its with my credentials

An error occurred: can only concatenate tuple (not "str") to tuple Traceback (most recent call last): File "C:\Users\plexy\AppData\Local\Programs\Python\Python312\Lib\site-packages\spotipy\client.py", line 242, in _auth_headers token = self.auth_manager.get_access_token(as_dict=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\plexy\AppData\Local\Programs\Python\Python312\Lib\site-packages\spotipy\oauth2.py", line 229, in get_access_token token_info = self._request_access_token() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\plexy\AppData\Local\Programs\Python\Python312\Lib\site-packages\spotipy\oauth2.py", line 238, in _request_access_token headers = _make_authorization_headers( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\plexy\AppData\Local\Programs\Python\Python312\Lib\site-packages\spotipy\oauth2.py", line 53, in _make_authorization_headers str(client_id + ":" + client_secret).encode("ascii")


TypeError: can only concatenate tuple (not "str") to tuple

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\plexy\Desktop\Discord Bot\Discord-bot-pip\Discord_bot.py", line 318, in play
    track = spotify.track(query)
            ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\plexy\AppData\Local\Programs\Python\Python312\Lib\site-packages\spotipy\client.py", line 375, in track
    return self._get("tracks/" + trid, market=market)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\plexy\AppData\Local\Programs\Python\Python312\Lib\site-packages\spotipy\client.py", line 327, in _get
    return self._internal_call("GET", url, payload, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\plexy\AppData\Local\Programs\Python\Python312\Lib\site-packages\spotipy\client.py", line 251, in _internal_call
    headers = self._auth_headers()
              ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\plexy\AppData\Local\Programs\Python\Python312\Lib\site-packages\spotipy\client.py", line 244, in _auth_headers
    token = self.auth_manager.get_access_token()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\plexy\AppData\Local\Programs\Python\Python312\Lib\site-packages\spotipy\oauth2.py", line 229, in get_access_token
    token_info = self._request_access_token()
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\plexy\AppData\Local\Programs\Python\Python312\Lib\site-packages\spotipy\oauth2.py", line 238, in _request_access_token
    headers = _make_authorization_headers(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\plexy\AppData\Local\Programs\Python\Python312\Lib\site-packages\spotipy\oauth2.py", line 53, in _make_authorization_headers
    str(client_id + ":" + client_secret).encode("ascii")
        ~~~~~~~~~~^~~~~
TypeError: can only concatenate tuple (not "str") to tuple

this is how i set the credentials:

```
spotify = spotipy.Spotify(
    client_credentials_manager=SpotifyClientCredentials(
        client_id=SPOTIFY_CLIENT_ID, 
        client_secret=SPOTIFY_CLIENT_SECRET
    )
)
```
olistori commented 1 month ago

It was a problem with my credential files, its contained extra "," after each credential. it fixed the problem. Sorry to bother