Search code examples
pythonspotify

How to bypass Spotipy prompting for link in terminal?


I am trying to build a website which shows the user some of their statistics on Spotify using the Spotipy python module. I am authenticating my application using:

username = "SomeUsername"
scope = "user-read-playback-state user-modify-playback-state user-top-read user-read-recently-played"
redirect_uri = "http://localhost/"
token = spotipy.util.prompt_for_user_token(
    username, scope, CLIENT_ID, CLIENT_SECRET, redirect_uri)
session = spotipy.Spotify(auth=token)

A page is then opened where the user grants permission for the app to access the required data, then the user is redirected to redirect_url. Then in the terminal, the user is prompted to paste the url that they were directed to. This is a problem since I don't want end users to have to do this, and I don't even know if they can. I'm sure that there is a solution to this, but I just couldn't find it.


Solution

  • I use this in my code:

    import spotipy
    from spotipy.oauth2 import SpotifyOAuth
    # your variables here 
    sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=SCOPE,client_id=CLIENT_ID,client_secret=CLIENT_SECRET,redirect_uri=REDIRECT_URI))
    

    If you have set environment variables, you can simply use:

    import spotipy
    from spotipy.oauth2 import SpotifyOAuth   
    SCOPE = 'your-scopes'
    spotipy.Spotify(auth_manager=SpotifyOAuth(scope=SCOPE))