this post was submitted on 26 Jun 2023
7 points (100.0% liked)

Python

6394 readers
5 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS
7
submitted 1 year ago* (last edited 1 year ago) by Hammerheart to c/python
 

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.

top 7 comments
sorted by: hot top controversial new old
[–] Hammerheart 4 points 1 year ago* (last edited 1 year ago) (1 children)

I was formatting my query poorly >:o

specifically, I was too clever by formatting my query like it appears in the URL of the response (ie: replacing spaces with +'s)

Also, the token was working the whole time, I think I was getting the error because I was attempting to access the search results by clicking the "href" in the response, and in doing was I was hitting the endpoint without providing the right header, which caused me to get the 401 error in my browser.

[–] jnovinger 2 points 1 year ago (1 children)

Hey, sorry we missed helping out!

Did you get everything sorted? Mind adding a "[Solved]" to the beginning of your post title if so?

[–] Hammerheart 1 points 1 year ago (1 children)

Yea I did, it wasn't my intention when I made the post but I basically got the rubber duck experience. I've edited the title.

[–] jnovinger 3 points 1 year ago

Rubber ducking is the best. Appreciate that, hopefully it helps start a trend.

[–] jnovinger 2 points 1 year ago (1 children)

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.

[–] Hammerheart 3 points 1 year ago (1 children)

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.

[–] jnovinger 3 points 1 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.