[GH-ISSUE #710] Can't get a token with Spotipy running with Kafka #423

Closed
opened 2026-02-27 23:22:32 +03:00 by kerem · 4 comments
Owner

Originally created by @giopri13 on GitHub (Jul 20, 2021).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/710

Hello everyone, I'm sorry to bother you with a question that has already been asked, but I have no clue on how to implement what I need.

I'm writing a script that retrieves the songs from the Top 50 Global playlist and sends a dictionary through Kafka. Locally, my script works because I could obtain the access token through the standard process of opening the browser and pasting the link.
The problem is that I need to run it within a docker container as a Kafka python app, so I can't really access a browser.
I tried both util.prompt_for_user_token() and oauth2.SpotifyClientCredentials().
The first option asks me to insert the URL I should have been redirected to, while the second one doesn't work with error "spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/playlists/37i9dQZEVXbMDoHDwVN2tF?additional_types=track:
Only valid bearer authentication supported, reason: None"
redirect_uri is the same as of My Developer Dashboard from Spotify.

Again, I'm sorry to ask such a question again (and also if I made any mistakes writing this).
Thank you for your attention.

Here's my code responsible for initializing Spotipy:

`
def init():

client_id = "xxxxxx"
client_secret = "xxxxx"
username = "xxxx"
scope = ""
redirect_uri = "https://localhost:1024/"

token = util.prompt_for_user_token(username, 
scope, 
client_id, 
client_secret,
redirect_uri)
"""
credentials = oauth2.SpotifyClientCredentials(
    client_id,
    client_secret)
token = credentials.get_access_token()
"""

spotify = spotipy.Spotify(token)
return spotify

`

Originally created by @giopri13 on GitHub (Jul 20, 2021). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/710 Hello everyone, I'm sorry to bother you with a question that has already been asked, but I have no clue on how to implement what I need. I'm writing a script that retrieves the songs from the Top 50 Global playlist and sends a dictionary through Kafka. Locally, my script works because I could obtain the access token through the standard process of opening the browser and pasting the link. The problem is that I need to run it within a docker container as a Kafka python app, so I can't really access a browser. I tried both util.prompt_for_user_token() and oauth2.SpotifyClientCredentials(). The first option asks me to insert the URL I should have been redirected to, while the second one doesn't work with error "spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/playlists/37i9dQZEVXbMDoHDwVN2tF?additional_types=track: Only valid bearer authentication supported, reason: None" redirect_uri is the same as of My Developer Dashboard from Spotify. Again, I'm sorry to ask such a question again (and also if I made any mistakes writing this). Thank you for your attention. Here's my code responsible for initializing Spotipy: ` def init(): client_id = "xxxxxx" client_secret = "xxxxx" username = "xxxx" scope = "" redirect_uri = "https://localhost:1024/" token = util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri) """ credentials = oauth2.SpotifyClientCredentials( client_id, client_secret) token = credentials.get_access_token() """ spotify = spotipy.Spotify(token) return spotify `
kerem 2026-02-27 23:22:32 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@smtchahal commented on GitHub (Aug 18, 2021):

@giopri13 Did you find a solution for this? I have the same problem.

<!-- gh-comment-id:901298495 --> @smtchahal commented on GitHub (Aug 18, 2021): @giopri13 Did you find a solution for this? I have the same problem.
Author
Owner

@giopri13 commented on GitHub (Aug 18, 2021):

@giopri13 Did you find a solution for this? I have the same problem.

Hello @smtchahal, in the end I managed to make it work not by using util.prompt_for_user_token but with SpotifyOAuth and setting the flag open_browser=False.
I had already tried going that route before submitting this issue but apparently python didn't like the way I was separating lines of code and threw errors at me for not writing on a single line.

Here's my solution

# Initialize Spotify instance def init(): client_id = "xxx" client_secret = "xxx" scope = "" redirect_uri = "https://spotify.com" spotify = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope=scope, open_browser=False)) return spotify

Now you'll be able to open manually the link for authorization and then paste your authorization code on the terminal

<!-- gh-comment-id:901312892 --> @giopri13 commented on GitHub (Aug 18, 2021): > @giopri13 Did you find a solution for this? I have the same problem. Hello @smtchahal, in the end I managed to make it work not by using util.prompt_for_user_token but with SpotifyOAuth and setting the flag open_browser=False. I had already tried going that route before submitting this issue but apparently python didn't like the way I was separating lines of code and threw errors at me for not writing on a single line. Here's my solution `# Initialize Spotify instance def init(): client_id = "xxx" client_secret = "xxx" scope = "" redirect_uri = "https://spotify.com" spotify = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope=scope, open_browser=False)) return spotify` Now you'll be able to open manually the link for authorization and then paste your authorization code on the terminal
Author
Owner

@smtchahal commented on GitHub (Aug 18, 2021):

SpotifyOAuth with open_browser=False did the trick, many thanks! :)

<!-- gh-comment-id:901315593 --> @smtchahal commented on GitHub (Aug 18, 2021): `SpotifyOAuth` with `open_browser=False` did the trick, many thanks! :)
Author
Owner

@Peter-Schorn commented on GitHub (Aug 18, 2021):

In order to authorize your app, you need to open the browser at least once. There's no way around that. Period. You can authorize your app the first time in a local script, and then save the token info in persistent storage, which you can make accessible from kafka. More specifically, after you authorize your app, a file called .cache will be created in the same directory as your script. This file contains the token info.

<!-- gh-comment-id:901321495 --> @Peter-Schorn commented on GitHub (Aug 18, 2021): In order to authorize your app, you need to open the browser at least _once_. There's no way around that. Period. You can authorize your app the first time in a local script, and then save the token info in persistent storage, which you can make accessible from kafka. More specifically, after you authorize your app, a file called `.cache` will be created in the same directory as your script. This file contains the token info.
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#423
No description provided.