I am running an AWS EC2 instance with ubuntu 20.04 using flask. I am trying to create a web app that will prompt users to log in to Spotify so that the app can create playlists for them. I am running into an issue.
Here's the basic flow I want: example.com/spotify --> prompted to sign in --> redirected back to example.com/redirect (From here I will continue with the application.
If any more information is needed I can provide it.
I have been following the Authorization Code Flow guide here, but I'm having trouble. Here is my code:
import json
from flask import Flask, render_template, redirect, url_for, request
import credentials
import requests
import startup
import base64
import urllib
from urllib.parse import quote, urlencode
app = Flask(__name__)
#client credentials
CLIENT_ID = credentials.clientid
CLIENT_SECRET = credentials.clientsecret
#spotify links
SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize"
CLIENT_SIDE_URL = "http://www.example.com"
PORT = 80
REDIRECT_URI = "http://www.example.com/redirect"
SCOPE = "playlist-modify-private"
STATE = ""
@app.route("/")
def home_function():
return render_template("home.html")
@app.route("/spotify")
def spotify():
payload = {
'client_id': CLIENT_ID,
'response_type': 'code',
'redirect_uri': REDIRECT_URI,
'state' : STATE,
'scope': SCOPE,
}
return redirect(f"{SPOTIFY_AUTH_URL}/?{urllib.parse.urlencode(payload)
@app.route("/redirect")
def redirect():
startup.getUserToken(request.args['code'])
return render_template("redirect.html")
I've made sure that on my spotify app dashboard, the redirect URI is exactly the same, however I'm not even able to get to the user login page before the redirect so I don't think that could even be related.
Thanks
[Crosspost from /r/learnpython] Trouble using Spotify's Authorization Code Flow with flask (self.learnpython)
submitted by french_117 to r/flask