spotipy-dev / spotipy

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

AttributeError: 'dict' object has no attribute 'get_access_token' #1002

Closed Zhiming77 closed 4 months ago

Zhiming77 commented 1 year ago

I apologize, in advance, if this issue is inherently noob-ish. However, after solving my issue(s) relevant to the EOFError issue described here: https://github.com/spotipy-dev/spotipy/issues/632.

My bot is "successfully" hosted on Heroku, and I can use commands that don't access the Spotify API. However, now when I try to access the API in any manner, for example, playlists = sp.current_user_playlists(), I am getting the following error:

web.1: File "/app/.heroku/python/lib/python3.11/site-packages/spotipy/client.py", line 338, in _put web.1: return self._internal_call("PUT", url, payload, kwargs)

web.1: File "/app/.heroku/python/lib/python3.11/site-packages/spotipy/client.py", line 247, in _internal_call web.1: headers = self._auth_headers()

web.1: File "/app/.heroku/python/lib/python3.11/site-packages/spotipy/client.py", line 238, in _auth_headers web.1: token = self.auth_manager.get_access_token(as_dict=False)

The code I used to solve the issue described in the above link is as follows (and uses the MemoryCacheHandler):

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope=scope, show_dialog=False, open_browser=False, cache_handler=MemoryCacheHandler(token_info=token_info) ).get_access_token(as_dict=True, check_cache=True))

Why is self.auth_manager appearing as a dictionary herein (it would seem based on the spotipy code that it should not be appearing as a dictionary)?

Thank you in advance for any and all assistance!

jeffp123 commented 1 year ago

I think the error is because you are passing SpotifyOAuth(...).get_access_token(...) as the auth_manager param.

Try this instead? (reformatted for clarity and removed the .get_access_token(as_dict=True, check_cache=True))

sp = spotipy.Spotify(
  auth_manager=SpotifyOAuth(
    client_id=client_id,
    client_secret=client_secret,
    redirect_uri=redirect_uri,
    scope=scope,
    show_dialog=False,
    open_browser=False,
    cache_handler=MemoryCacheHandler(token_info=token_info),
  )
)
stephanebruckert commented 4 months ago

Looks like a solution was provided, closing