Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Need help with my web app

Need help with my web app

I am creating a flask application that is supposed to create a playlist in my account. The code below works when I am signed in, but when my friend signed in and tried to create a playlist, it was still being made in my account. If anyone could help me figure out why it would be greatly appreciated. Thanks!

 

from flask import Flask, request, redirect, url_for, session, render_template
from spotipy import Spotify
from spotipy.oauth2 import SpotifyOAuth
import time

app = Flask(__name__)

app.config['SESSION_COOKIE_NAME'] = 'Spotify Cookie'
app.secret_key = 'my_secret_key'
TOKEN_INFO = 'token'

@app.route('/')
def index():
return render_template("login.html")

@app.route('/login')
def login():
auth_url = create_spotify_oauth().get_authorize_url()
return redirect(auth_url)

@app.route('/callback')
def callback():
session.clear()
auth_code = request.args.get('code')
token_info = create_spotify_oauth().get_access_token(auth_code)
session[TOKEN_INFO] = token_info
return redirect(url_for('setUp'))

@app.route('/setUp')
def setUp():
return render_template("setUp.html")

@app.route('/create_playlist', methods=['POST'])
def create_playlist():
try:
token_info = get_token()
except:
return redirect('/')

sp = Spotify(auth=token_info['access_token'])
title = request.form.get('title')
genre = request.form.get('genre')
num_songs = request.form.get('num_songs')
new_playlist = sp.user_playlist_create(user=sp.current_user()['id'], name=title, public=True, description="Playlist created by TuneSwipe")
return str(sp.current_user())



def create_spotify_oauth():
return SpotifyOAuth(
client_id='my_client_id',
client_secret='my_client_secret',
redirect_uri=url_for('callback', _external=True),
scope="user-library-read playlist-modify-public playlist-modify-private")

def get_token():
token_info = session.get(TOKEN_INFO, None)
if not token_info:
redirect(url_for('login'))
expired = token_info['expires_at'] - int(time.time()) < 60
if(expired):
token_info = create_spotify_oauth().refresh_access_token(token_info['refresh token'])
return token_info

if __name__ == "__main__":
app.run(debug=True)
Reply
0 Replies

Suggested posts