spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
5.03k stars 957 forks source link

Spotify Authentication Error in Docker #1089

Closed schebook closed 6 months ago

schebook commented 6 months ago

Hi All,

Trying to containerize my spotipy program in Docker so that I can host it on my Linux server. This is my first time working with Docker and said Linux server, so please bear with me.

Below is my code:

import spotipy
from spotipy.oauth2 import SpotifyOAuth
import os
import json

def main():
    global spotify
    #Getting the client ID and client secret after making them environment varibales
    client_id = os.environ.get('SPOTIPY_CLIENT_ID')
    client_secret = os.environ.get('SPOTIPY_CLIENT_SECRET')
    client_redirect = os.environ.get('SPOTIPY_REDIRECT_URI')

    #Performing a check to make sure that the environment variables are remembered.
    if client_id is None or client_secret is None:
        print("ERROR: Set SPOTIPY_CLIENT_ID and SPOTIPY_CLIENT SECRET environment variables")
        return

    #Entering the URI of the playlist
    scope = "playlist-read-private, playlist-modify-private, playlist-modify-public"
    spotify = spotipy.Spotify(client_credentials_manager=SpotifyOAuth(scope=scope))

    playlist1 = spotify.playlist_tracks('37i9dQZF1EJzGGGOWOiITB')
    playlist2 = spotify.playlist_tracks('6CPMN5JBkcD4Sk9vD0OIOA')

    uris = parsep1(playlist1)
    addp2(playlist2, uris)

"""
Input: playlist1
Output: ids
This function serves to parse through a playlist and return all of the song IDs.
"""

def parsep1(playlist1):
    uris = []
    for item in playlist1['items']:
        track = item['track']
        uri = track['uri']
        uris.append(uri)
    return uris

"""
Input: playlist2, ids
Output: None
This function serves to add songs from one playlist to another.
"""

def addp2(playlist2, uris):
    existing_uris = [item['track']['uri'] for item in playlist2['items']]
    uris_to_add = [uri for uri in uris if uri not in existing_uris]
    if uris_to_add:
        spotify.playlist_add_items('6CPMN5JBkcD4Sk9vD0OIOA', uris_to_add)

if __name__ == "__main__":
    main()

And this is what I have going on in my Dockerfile:

# Dockerfile, Image, Container
FROM python:3.12.3

ADD spotify.py .

RUN pip install spotipy

CMD ["python", "./spotify.py"]

I build my spotify instance in docker and then run the following to establish my environment variables:

docker run -e SPOTIPY_CLIENT_ID=<redacted> -e SPOTIPY_CLIENT_SECRET=<redacted> -e SPOTIPY_REDIRECT_URI=https://localhost:8888/callback spotify

The error that I'm getting suggests that there's an issue when spotify's authentication tries to launch a webpage. Is there a way to get around this? Eager to learn! Thanks.

schebook commented 6 months ago

I'll also add that I attempted to use the client credentials manager but this unfortunately does not recognize Spotify created playlist IDs.

dieser-niko commented 6 months ago

You can pass open_browser=False to the SpotifyOAuth constructor

dieser-niko commented 6 months ago

Any updates yet? I should also add that you might want to expose the port for the redirect url if you didn't do that already.

schebook commented 6 months ago

Hey @dieser-niko ! I've been reworking my whole code and making it a bit more efficient / easier to manage. I'm going to try to dockerize it by giving it access to my .cache file and see if it will no longer have to authorize.

I opened the port but no luck with that. If all else fails, I may try to create a flask server to handle authentication.

dieser-niko commented 6 months ago

That normally shouldn't be a problem. I can help you with Docker in about 3 to 4 hours if you want to.

schebook commented 6 months ago

I appreciate your help! I actually got it to containerize and run within Docker. Passing the .cache file into the container allows it to ride off the former authentication.