[GH-ISSUE #1089] Spotify Authentication Error in Docker #646

Closed
opened 2026-02-28 00:00:31 +03:00 by kerem · 6 comments
Owner

Originally created by @schebook on GitHub (Apr 26, 2024).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1089

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.

Originally created by @schebook on GitHub (Apr 26, 2024). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1089 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: ```python 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: ```python # 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.
kerem 2026-02-28 00:00:31 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@schebook commented on GitHub (Apr 26, 2024):

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

<!-- gh-comment-id:2080108395 --> @schebook commented on GitHub (Apr 26, 2024): I'll also add that I attempted to use the client credentials manager but this unfortunately does not recognize Spotify created playlist IDs.
Author
Owner

@dieser-niko commented on GitHub (Apr 27, 2024):

You can pass open_browser=False to the SpotifyOAuth constructor

<!-- gh-comment-id:2081187150 --> @dieser-niko commented on GitHub (Apr 27, 2024): You can pass `open_browser=False` to the SpotifyOAuth constructor
Author
Owner

@dieser-niko commented on GitHub (May 3, 2024):

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.

<!-- gh-comment-id:2092739353 --> @dieser-niko commented on GitHub (May 3, 2024): 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.
Author
Owner

@schebook commented on GitHub (May 3, 2024):

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.

<!-- gh-comment-id:2093532316 --> @schebook commented on GitHub (May 3, 2024): 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.
Author
Owner

@dieser-niko commented on GitHub (May 3, 2024):

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

<!-- gh-comment-id:2093596157 --> @dieser-niko commented on GitHub (May 3, 2024): That normally shouldn't be a problem. I can help you with Docker in about 3 to 4 hours if you want to.
Author
Owner

@schebook commented on GitHub (May 3, 2024):

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.

<!-- gh-comment-id:2093710871 --> @schebook commented on GitHub (May 3, 2024): 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.
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#646
No description provided.