spotipy: Error when authenticating with credentials in code

I get an error when authenticating with credentials in code, as opposed to env variables. The client works when using env variables.

from config import APP_CLIENT_ID, APP_CLIENT_SECRET

auth_manager = SpotifyClientCredentials( client_id = APP_CLIENT_SECRET, client_secret = APP_CLIENT_SECRET )
sp = spotipy.Spotify( auth_manager = auth_manager )

C:\usr\Miniconda3\lib\site-packages\spotipy\oauth2.py in _request_access_token(self)
    267             return token_info
    268         except requests.exceptions.HTTPError as http_error:
--> 269             self._handle_oauth_error(http_error)
    270
    271     def _add_custom_values_to_token_info(self, token_info):

C:\usr\Miniconda3\lib\site-packages\spotipy\oauth2.py in _handle_oauth_error(self, http_error)
    144             error_description = None
    145
--> 146         raise SpotifyOauthError(
    147             'error: {0}, error_description: {1}'.format(
    148                 error, error_description

SpotifyOauthError: error: invalid_client, error_description: Failed to get client

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 20 (10 by maintainers)

Most upvoted comments

I can confirm that during the SpotifyOAuth() execution ENV is available and correct (https://github.com/spotipy-dev/spotipy/issues/856#issuecomment-1434506569).

Have you also confirmed that your app can also access the client secret? Furthermore, I must admit that, if you’re still getting an invalid_client error, then I’m still not confident that spotipy can access both the client id and client secret. For the sake of simplicity if nothing else, you should initialize your instance of Spotify as follows. (check if you have extraneous whitespace in your client_id and client_secret variables.)

client_id = os.getenv('SPOTIPY_CLIENT_ID')
client_secret = os.getenv('SPOTIPY_CLIENT_SECRET')
print(f"client id: '{client_id}'")
print(f"client secret: '{client_secret}'")
sp = spotipy.Spotify(
    auth_manager=SpotifyOAuth(
        client_id=client_id, client_secret=client_secret, scope=scope
    )
)

I suppose that it should use the flow described at Spotify docs:

The Spotify web API has multiple authorization flows (see this article).The Client Credentials Flow does not support authorizing with a user, and so can not be used with endpoints that access/modify user data. It says that right at the top of the page you linked:

The Client Credentials flow is used in server-to-server authentication. Since this flow does not include authorization, only endpoints that do not access user information can be accessed.

spotipy does support this authorization method too: https://github.com/spotipy-dev/spotipy/blob/572195617b3d63f05f4083a4067fe6eb0dfe448b/spotipy/oauth2.py#L160

If you want you authorize with a user, you must open a URL in the browser. Period. All of the authorization flows that support authorizing with a user require opening a URL in the browser. The reason for this is simple: The Spotify user must be given a chance to login to their Spotify account and grant your app permission to access/modify their data. They can’t login to their account directly in your app because then you could steal their username and password, so they have to login on the browser.