繁体   English   中英

Spotify 授权码流程:无法进入初始用户登录,内部服务器错误

[英]Spotify Authorization code Flow: Can't get to initial user login, Internal Server Error

我正在使用 flask 运行具有 ubuntu 20.04 的 AWS EC2 实例。 我正在尝试创建一个 web 应用程序,该应用程序将提示用户登录 Spotify,以便该应用程序可以为他们创建播放列表。 我遇到了一个问题。

这是我想要的基本流程:example.com/spotify --> 提示登录 --> 重定向回 example.com/redirect (从这里我将继续应用程序。

如果需要更多信息,我可以提供。

我一直在关注授权代码流程指南但我遇到了麻烦。 这是我的代码:

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")

我已经确保在我的 spotify 应用程序仪表板上,重定向 URI 完全相同,但是我什至无法在重定向之前访问用户登录页面,所以我认为这甚至不相关。

您可以尝试手动编码,如下所示?

f"{SPOTIFY_AUTH_URL}/?client_id={CLIENT_ID}&response_type={CODE}&redirect_uri={REDIRECT_URI}&scope={SCOPE}&state={STATE}}"

如果这不能为您解决问题,请告诉我。

您可以使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM