I wrote this Flask API in order to interact with my app's front-end and the Spotify API. Locally, the API runns fine however when I try to deploy it to Google Cloud and run it, the following error from the Spotipy package shows up:
"POST /createPlaylist" 500
ERROR in app: Exception on /createPlaylist [POST]
Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.11/site-packages/spotipy/oauth2.py", line 115, in _get_user_input return raw_input(prompt)
^^^^^^^^^
NameError: name 'raw_input' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 2190, in wsgi_app response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 1486, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 1484, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 1469, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/main.py", line 43, in create_playlist
creation_response = sp.user_playlist_create(
...
I did some research on this and I found this GitHub example to adapt the API for the server: https://github.com/spotipy-dev/spotipy/blob/master/examples/app.py, however, couldn't solve the issue.
Here's the code I have so far:
from flask import Flask, request, redirect, jsonify, session
from flask_session import Session
import os
import spotipy
from spotipy.oauth2 import SpotifyOAuth, SpotifyClientCredentials
from concurrent.futures import ThreadPoolExecutor, as_completed
from interactions import (
filterTracks,
generateCustomTitle,
generateNewTracksForReinforcement,
)
app = Flask(__name__)
scopes = [
"ugc-image-upload",
"playlist-read-private",
"playlist-read-collaborative",
"playlist-modify-public",
"playlist-modify-private",
]
client_id_var = os.getenv("SPOTIFY_CLIENT_ID")
client_secret_var = os.getenv("SPOTIFY_CLIENT_SECRET")
client_credentials_manager = SpotifyClientCredentials(
client_id=client_id_var, client_secret=client_secret_var
)
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
sp = spotipy.Spotify(
client_credentials_manager=client_credentials_manager,
auth_manager=SpotifyOAuth(
client_id=client_id_var,
client_secret=client_secret_var,
redirect_uri="https://sound-scout-flask-api.nw.r.appspot.com/callback",
scope=scopes,
show_dialog=True,
cache_handler=spotipy.FlaskSessionCacheHandler(app),
),
)
@app.route("/")
def sign_in():
auth_url = sp.auth_manager.get_authorize_url()
return redirect(auth_url)
@app.route("/callback")
def callback():
sp.auth_manager.get_access_token(request.args.get("code"))
return redirect("https://sound-scout-flask-api.nw.r.appspot.com/createPlaylist")
@app.route("/createPlaylist", methods=["POST"])
def create_playlist():
data = request.get_json()
promptAnswer = data.get("promptAnswer")
playlistSubject = data.get("playlistSubject")
playlistTitle = generateCustomTitle(promptAnswer).replace('"', "")
tracks = filterTracks(promptAnswer)
user_id = sp.current_user()["id"] # Get current user's ID
creation_response = sp.user_playlist_create(
user=user_id,
name=playlistTitle,
public=False,
collaborative=True,
description=playlistSubject,
)
if creation_response:
playlist_id = creation_response["id"]
playlistURL = creation_response["external_urls"]["spotify"]
track_ids = getTrackIds(tracks)
result_url = addTrackToPlaylist(playlist_id, playlistURL, track_ids)
reinforcePlaylistResults(playlistURL, 15, promptAnswer, playlistSubject)
return jsonify({"playlistURL": result_url}), 200
else:
return jsonify({"error": "Playlist creation failed"}), 400
def getTrackId(track_name: str, track_artist: str):
...
if __name__ == "__main__":
app.run(debug=True)