Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Spotify is randomly timing out my API requests?

Spotify is randomly timing out my API requests?

I have made an app in python that needs to update a UI with currently playing song info around every second (I have a progress bar for the songs duration) but my requests are randomly timed out with spotify returning a 401. I use spotipy to interact with the API and I have added my account to my dashboard, but no dice. Any suggestions would be appreciated. I have attached the error and here is my code that requests the API:

 

username = 'apple11533334334343'
scope = 'user-read-currently-playing'

token = util.prompt_for_user_token(username, scope, redirect_uri='http://localhost:8888/callback')

spotify = spotipy.Spotify(auth=token, requests_timeout=10, retries=10)

image_data = requests.get('https://i.scdn.co/image/ab67616d000048515f1590fb5d2f9e1bed8bb2e8').content #creates a default image to be changed since get_info doesnt update fast enough
track_name = str()
artists_name = str()
album_name = str()
progress = 0.1
total_length = 0.1

def get_info():

while True:

try:

info = spotify.current_user_playing_track()

#print(json.dumps(info, indent=4))

global image_data
global track_name
global artists_name
global album_name
global progress
global total_length

#get the data from the json returned
track_name = info['item']['name']
artists = [artist for artist in info['item']['artists']]
artists_name = ', '.join([artist['name'] for artist in artists])
image_info = info['item']['album']['images'][1]
album_name = info['item']['album']['name']
progress_ms = info['progress_ms']
track_length = info['item']['duration_ms']
track_url = info['item']['external_urls']['spotify']

#make variables to use in app
track_name = 'Song Title: ' + track_name
artists_name = 'Artists: ' + artists_name
album_name = 'Album Name: ' + album_name

#convert the time to s for progress bar
progress = progress_ms / 1000
total_length = track_length / 1000

#get the image from the url to put in app
image_url = image_info['url']
image_data = requests.get(image_url).content

except TypeError:
track_name = 'No Song Playing'
artists_name = '...'
album_name = '...'
except socket.gaierror:
print('Connection Error, try connecting wifi dumbass')
except ConnectionError as e:
print(f"Connection error encountered {e}")
except TimeoutError:
print("Timeout Error")
Reply
2 Replies

401 means that the access token is expired. This happens after an hour.

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

An hour of cumulative use? Because it happens at random times after I start making new requests. For example I have had times where I start the program and 30 seconds later I get 401, and then other times it takes like 30 minutes.

Suggested posts

Type a product name