[GH-ISSUE #1067] DeprecationWarning in get_access_token() when using SpotifyOAuth #628

Closed
opened 2026-02-28 00:00:23 +03:00 by kerem · 1 comment
Owner

Originally created by @beeeegi on GitHub (Jan 2, 2024).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1067

Description of the bug
A DeprecationWarning is encountered when using sp_oauth.get_access_token() with the following message:

DeprecationWarning: You're using 'as_dict = True'. get_access_token will return the token string directly in future versions. Please adjust your code accordingly, or use get_cached_token instead.
token_info = sp_oauth.get_access_token()

Code

def refresh_token_if_expired():
    token_info = sp_oauth.get_access_token()

    if token_info and sp_oauth.is_token_expired(token_info):
        new_token_info = sp_oauth.refresh_access_token(token_info['refresh_token'])
        new_token = new_token_info['access_token']
        return new_token
    elif token_info:
        return token_info['access_token']
    else:
        return None

Expected behavior
I expected to use sp_oauth.get_access_token() without encountering a DeprecationWarning. The warning is suggesting a change in how the method is used.

Output

DeprecationWarning: You're using 'as_dict = True'. get_access_token will return the token string directly in future versions. Please adjust your code accordingly, or use get_cached_token instead.
token_info = sp_oauth.get_access_token()

Environment:

  • OS: Windows
  • Python version 3.10.11
  • spotipy version 2.22.1
  • IDE: Visual Studio Code

Additional context
I am encountering a warning message in my code despite its functionality being correct. The warning states:

DeprecationWarning: You're using 'as_dict = True'.get_access_token will return the token string directly in future versions. Please adjust your code accordingly, or use get_cached_token instead.

While my code appears to work as expected, I want to confirm whether this warning is indicative of a potential issue in my usage of the spotipy library or if it is an expected behavior. I appreciate any guidance on whether any adjustments are needed in my code.

Originally created by @beeeegi on GitHub (Jan 2, 2024). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1067 **Description of the bug** A DeprecationWarning is encountered when using `sp_oauth.get_access_token()` with the following message: > DeprecationWarning: You're using 'as_dict = True'. get_access_token will return the token string directly in future versions. Please adjust your code accordingly, or use get_cached_token instead. > token_info = sp_oauth.get_access_token() **Code** ```py def refresh_token_if_expired(): token_info = sp_oauth.get_access_token() if token_info and sp_oauth.is_token_expired(token_info): new_token_info = sp_oauth.refresh_access_token(token_info['refresh_token']) new_token = new_token_info['access_token'] return new_token elif token_info: return token_info['access_token'] else: return None ``` **Expected behavior** I expected to use `sp_oauth.get_access_token()` without encountering a `DeprecationWarning`. The warning is suggesting a change in how the method is used. **Output** ``` DeprecationWarning: You're using 'as_dict = True'. get_access_token will return the token string directly in future versions. Please adjust your code accordingly, or use get_cached_token instead. token_info = sp_oauth.get_access_token() ``` **Environment:** - OS: Windows - Python version 3.10.11 - spotipy version 2.22.1 - IDE: Visual Studio Code **Additional context** I am encountering a warning message in my code despite its functionality being correct. The warning states: > DeprecationWarning: You're using 'as_dict = True'.get_access_token will return the token string directly in future versions. Please adjust your code accordingly, or use get_cached_token instead. While my code appears to work as expected, I want to confirm whether this warning is indicative of a potential issue in my usage of the spotipy library or if it is an expected behavior. I appreciate any guidance on whether any adjustments are needed in my code.
kerem 2026-02-28 00:00:23 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@Jack-Dane commented on GitHub (Jan 2, 2024):

@beeeegi, I don't support this project but I think I can provide some insight based on looking at the code.

It looks like based on the message, this function will just return the access token as a string instead of the dictionary in later versions of the package. The kwarg and warning provide a way for users to upgrade their Spotipy package without making a breaking change to existing code. You don't need to be concerned about this if you have no plans to upgrade the package.

But it also looks like the get_access_token function will automatically refresh the token if it has expired, so I don't see a need for you to implement this yourself. It looks like you could just use the access_token = sp_oauth.refresh_access_token(as_dict=False).

github.com/spotipy-dev/spotipy@a14a28e10c/spotipy/oauth2.py (L523-L531)

<!-- gh-comment-id:1874600489 --> @Jack-Dane commented on GitHub (Jan 2, 2024): @beeeegi, I don't support this project but I think I can provide some insight based on looking at the code. It looks like based on the message, this function will just return the access token as a string instead of the dictionary in later versions of the package. The `kwarg` and warning provide a way for users to upgrade their Spotipy package without making a breaking change to existing code. You don't need to be concerned about this if you have no plans to upgrade the package. But it also looks like the `get_access_token` function will automatically refresh the token if it has expired, so I don't see a need for you to implement this yourself. It looks like you could just use the `access_token = sp_oauth.refresh_access_token(as_dict=False)`. https://github.com/spotipy-dev/spotipy/blob/a14a28e10c1889cce83eec7a7e1ad4b5944a452d/spotipy/oauth2.py#L523-L531
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#628
No description provided.