I am trying to create a playlist with spotify and the spotipy library in python. However, I keep getting a “No token provided” error when making my API request. However, if I use the same token with a curl request, it works! Can someone please help. This is my code:

auth_manager = SpotifyOAuth(client_id=CLIENT,
                            client_secret=SECRET,
                            redirect_uri="http://example.com/",
                            scope=SCOPE,
                            username=spotify_display_name
                            )
token = auth_manager.get_access_token(
    as_dict=False,
    check_cache=True
)

sp = spotipy.Spotify(auth_manager=auth_manager,
                     auth=token
                     )
user_dict = sp.current_user()
user_id = user_dict["id"]
print(f"Welcome, {user_dict['display_name']}")


# SEARCH
# QUERY FORMAT: "track: track-name year: YYYY"

spotify_search_endpoint = "https://api.spotify.com/v1/search/"
test_query = "track:Hangin'+Tough year:1989"

search_parameters = {
    "q": format_query(test_query),
    "type": "track"
}

results = sp.search(q=search_parameters["q"])
print(results)

output:

{'tracks': {'href': 'https://api.spotify.com/v1/search?query=track%3AHangin%27%2BTough%2520year%3A1989&type=track&offset=0&limit=10', 'items': [], 'limit': 10, 'next': None, 'offset': 0, 'previous': None, 'total': 0}}
{
"error": {
"status": 401,
"message": "No token provided"
}
}

This is really frustrating! The authentication is working, otherwise the token wouldn’t have been valid for the curl request. I must be doing something wrong with spotipy.

  • Jason NovingerM
    link
    21 year ago

    Curious, what kind of playlists are you creating?

    I have to admit I feel like I’m not imaginitive enough to need to script Spotify.

    • @HammerheartOP
      link
      31 year ago

      It makes a playlist based on data scraped from the billboard top 100 chart for a given day.

      I don’t know if I couldve come up with it on my own, it’s part of a Udemy course I’m doing.

      • Jason NovingerM
        link
        31 year ago

        Gotcha. That’s a fun alternative to the usual kind of course work.

        Thanks for contributing to the community! Look forward to hearing more from you as you continue.