最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Spotipy invalid_client error despite correct credentials and setup - Stack Overflow

programmeradmin3浏览0评论

I’m facing a persistent invalid_client error with the description Invalid client secret when using Spotipy to authenticate with the Spotify API. Even though my client_id, client_secret, and redirect_uri are correctly set up in both my code and the Spotify Developer Dashboard, the error persists. I’ve tried several troubleshooting steps (listed below), but nothing has worked so far.

My Setup: Spotipy version: 2.25.1 (latest) Python version: 3.12 Environment: macOS, running in VS Code with Anaconda base environment (/opt/anaconda3/bin/python) Code: Here’s the minimal script I’m using to test authentication:

import spotipy
from spotipy.oauth2 import SpotifyOAuth

# Credentials from Spotify Developer Dashboard
CLIENT_ID = 'my_client_id'
CLIENT_SECRET = 'my_client_secret'
REDIRECT_URI = ':3000'

# Authenticate
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    redirect_uri=REDIRECT_URI,
    scope="user-library-read"
))

# Test API call
results = sp.current_user_saved_tracks()
print("Fetched saved tracks successfully!")

when i run the code it opens Safari for authentication. After approving access, I get this error:

Using Python interpreter: /opt/anaconda3/bin/python
Fetching your saved tracks...
Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.12/site-packages/spotipy/oauth2.py", line 527, in get_access_token
    response.raise_for_status()
  File "/opt/anaconda3/lib/python3.12/site-packages/requests/models.py", line 1024, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: 

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/-/Downloads/spotipy_new.py", line 26, in <module>
    results = sp.current_user_saved_tracks()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/lib/python3.12/site-packages/spotipy/client.py", line 1324, in current_user_saved_tracks
    return self._get("me/tracks", limit=limit, offset=offset, market=market)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/lib/python3.12/site-packages/spotipy/client.py", line 327, in _get
    return self._internal_call("GET", url, payload, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/lib/python3.12/site-packages/spotipy/client.py", line 251, in _internal_call
    headers = self._auth_headers()
              ^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/lib/python3.12/site-packages/spotipy/client.py", line 242, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/lib/python3.12/site-packages/spotipy/oauth2.py", line 533, in get_access_token
    self._handle_oauth_error(http_error)
  File "/opt/anaconda3/lib/python3.12/site-packages/spotipy/oauth2.py", line 117, in _handle_oauth_error
    raise SpotifyOauthError(
spotipy.exceptions.SpotifyOauthError: error: invalid_client, error_description: Invalid client secret



Steps I’ve Already Taken:

Verified credentials: Confirmed client_id and client_secret match exactly with the Spotify Developer Dashboard.

  1. Checked redirect URI: Ensured :3000 is correctly registered in the dashboard (no extra spaces or slashes).

  2. Cleared cache: Removed the .cache file to force fresh authentication.

  3. Unset environment variables: Executed unset CLIENT_ID and unset CLIENT_SECRET to rule out conflicts.

  4. Updated Spotipy: Ran pip install --upgrade spotipy to ensure I’m on the latest version. Tested minimal script: Used the script above to isolate the issue, but it still fails.

  5. Checked for typos: Verified no leading/trailing spaces in credentials or URI.

  6. Additional Notes: The OAuth flow starts correctly (Safari opens, and I can log in), but the error occurs during the token exchange. I’m willing to recreate the Spotify app in the dashboard if needed, but I’d prefer to avoid that unless it’s the only solution.

  7. Question: What might be causing this invalid_client error despite my setup appearing correct? Are there additional debugging steps or fixes I can try to resolve this?

发布评论

评论列表(0)

  1. 暂无评论