[GH-ISSUE #209] AttributeError: 'SpotifyClientCredentials' object has no attribute 'is_token_expired' #106

Closed
opened 2026-02-27 23:20:52 +03:00 by kerem · 3 comments
Owner

Originally created by @ritiek on GitHub (Aug 23, 2017).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/209

I successfully authenticated via spotipy.oauth2.SpotifyClientCredentials(). The docs say calling method is_token_expired() on it should be valid but it gives:

AttributeError: 'SpotifyClientCredentials' object has no attribute 'is_token_expired'

However, calling get_access_token() works fine.

What is wrong with is_token_expired()?

Originally created by @ritiek on GitHub (Aug 23, 2017). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/209 I successfully authenticated via `spotipy.oauth2.SpotifyClientCredentials()`. The docs say [calling method ](http://spotipy.readthedocs.io/en/latest/#spotipy.oauth2.SpotifyClientCredentials.is_token_expired) `is_token_expired()` on it should be valid but it gives: > AttributeError: 'SpotifyClientCredentials' object has no attribute 'is_token_expired' However, calling ` get_access_token()` works fine. What is wrong with `is_token_expired()`?
kerem closed this issue 2026-02-27 23:20:52 +03:00
Author
Owner

@roblescoulter commented on GitHub (Oct 17, 2017):

Hello, I'm having the same issue. Were you able to solve it @ritiek ?

<!-- gh-comment-id:337123099 --> @roblescoulter commented on GitHub (Oct 17, 2017): Hello, I'm having the same issue. Were you able to solve it @ritiek ?
Author
Owner

@ritiek commented on GitHub (Oct 17, 2017):

I am using a try except block as an alternative. Something like below to refresh the token once it expires.

try:
    # usual code involving spotipy
except spotipy.oauth2.SpotifyOauthError:
    token = new_token()
    spotify = spotipy.Spotify(auth=token)
<!-- gh-comment-id:337140946 --> @ritiek commented on GitHub (Oct 17, 2017): I am using a `try` `except` block as an alternative. Something like below to refresh the token once it expires. ``` try: # usual code involving spotipy except spotipy.oauth2.SpotifyOauthError: token = new_token() spotify = spotipy.Spotify(auth=token) ```
Author
Owner

@ritiek commented on GitHub (Oct 17, 2017):

As a matter of fact, I just noticed that is_token_expired() is available only when using spotipy directly from the master branch.

It can be used with:

import spotipy.oauth2 as oauth2

creds = oauth2.SpotifyClientCredentials(client_id, client_secret)
token = creds.get_access_token()
oauth2.is_token_expired(creds.token_info)

As an alternative for old versions on pip, just make a small function (copying this from the master branch):

import spotipy.oauth2 as oauth2
import time

def is_token_expired(token_info):
    now = int(time.time())
    return token_info['expires_at'] - now < 60

creds = oauth2.SpotifyClientCredentials(client_id, client_secret)
token = creds.get_access_token()
is_token_expired(creds.token_info)

Closing this issue.

<!-- gh-comment-id:337235747 --> @ritiek commented on GitHub (Oct 17, 2017): As a matter of fact, I just noticed that `is_token_expired()` is available only when using `spotipy` directly from the master branch. It can be used with: ``` import spotipy.oauth2 as oauth2 creds = oauth2.SpotifyClientCredentials(client_id, client_secret) token = creds.get_access_token() oauth2.is_token_expired(creds.token_info) ``` As an alternative for old versions on pip, just make a small function (copying this from the master branch): ``` import spotipy.oauth2 as oauth2 import time def is_token_expired(token_info): now = int(time.time()) return token_info['expires_at'] - now < 60 creds = oauth2.SpotifyClientCredentials(client_id, client_secret) token = creds.get_access_token() is_token_expired(creds.token_info) ``` Closing this issue.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#106
No description provided.