简体   繁体   English

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

[英]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.

相关问题 Spotify API {'error': 'invalid_client'} 授权代码流 [400] - Spotify API {'error': 'invalid_client'} Authorization Code Flow [400] Spotify API 授权代码流失败({'error':'invalid_grant','error_description':'无效授权代码'}) - Spotify API Authorization Code Flow failing ({'error': 'invalid_grant', 'error_description': 'Invalid authorization code'}) Spotify授权码流程返回的响应不完整 - Spotify Authorization Code Flow returns incomplete response 授权代码流中的错误请求 Spotify - Bad Request at Authorization Code Flow Spotify Spotify API 授权代码流错误:“缺少 grant_type 参数”(Python) - Spotify API authorization code flow error: "grant_type parameter is missing" (Python) Spotipy 无法使用 SpotifyOAuth 访问用户信息(授权流程错误) - Spotipy can't access user's info using SpotifyOAuth (authorization flow error) Spotify授权代码-获取令牌[控制台应用程序] - Spotify authorization code - get token [ console application ] 如何在Spotify iOS SDK中获取“授权码” - How to get “authorization code” in Spotify iOS SDK Spotify - 使用隐式流获取和更改用户数据 - Spotify - get and change user data with Implicit Flow Spotify 授权代码(不是访问令牌)即将过期 - 我该如何避免这种情况? - Spotify authorization code (not access token) is expiring - how can I circumvent this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM