[英]Spotify Authorization code Flow: Can't get to initial user login, Internal Server Error
I am running an AWS EC2 instance with ubuntu 20.04 using flask.我正在使用 flask 运行具有 ubuntu 20.04 的 AWS EC2 实例。 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.
我正在尝试创建一个 web 应用程序,该应用程序将提示用户登录 Spotify,以便该应用程序可以为他们创建播放列表。 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.这是我想要的基本流程:example.com/spotify --> 提示登录 --> 重定向回 example.com/redirect (从这里我将继续应用程序。
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.我已经确保在我的 spotify 应用程序仪表板上,重定向 URI 完全相同,但是我什至无法在重定向之前访问用户登录页面,所以我认为这甚至不相关。
can you try encoding manually, like the following?您可以尝试手动编码,如下所示?
f"{SPOTIFY_AUTH_URL}/?client_id={CLIENT_ID}&response_type={CODE}&redirect_uri={REDIRECT_URI}&scope={SCOPE}&state={STATE}}" f"{SPOTIFY_AUTH_URL}/?client_id={CLIENT_ID}&response_type={CODE}&redirect_uri={REDIRECT_URI}&scope={SCOPE}&state={STATE}}"
Please let me know if this won't solve the problem for you.如果这不能为您解决问题,请告诉我。
You can use url_for: https://flask.palletsprojects.com/en/1.1.x/api/#flask.url_for您可以使用 url_for: https://flask.palletsprojects.com/en/1.1.x/api/#flask.url_for
spotify_url = url_for(
f'{SPOTIFY_AUTH_URL}',
client_id = client_id,
response_type ='code',
redirect_uri= redirect_uri,
state= state,
scope=scope
)
redirect(spotify_url)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.