2 回答

TA贡献1794条经验 获得超7个赞
Spotipy 的prompt_for_user_token
方法是作为一种快速而肮脏的方法提供的,以允许在本地使用该模块的人启动和运行,并且如果您希望任意用户能够通过网站对自己进行身份验证,则它不打算成为应该直接构建的代码. 由于您有一个 Flask 应用程序,您可能应该拥有它和您的 Spotipy 代码直接通过导入而不是使用管道和标准 i/o 进行交互。
需要研究的一些事情是:
Flask-Login如果您想管理登录网站的用户
将身份验证传递给
spotipy.Spotify
访问令牌(客户端凭据管理器或requests
会话)以外的替代方法。这些其他方法允许 Spotipy 客户端在访问令牌在使用中过期时刷新访问令牌,处理用户撤销等。requests-oauth2(请注意,这只是众多 OAuth2 Python 库中的一个,可能不是最好的),它使所有授权内容更加方便,并允许您执行以下操作:
# import Flask things, requests-oauth2,
from requests_oauth2 import OAuth2
from spotipy import Spotify
from flask import redirect
spotify_auth = OAuth2(
client_id=os.environ["SPOTIFY_CLIENT_ID"],
client_secret=os.environ["SPOTIFY_CLIENT_SECRET"],
redirect_uri=f"http://{os.environ['HOST_NAME']}/authorize",
site="https://accounts.spotify.com",
authorization_url="/authorize",
token_url="/api/token",
scope_sep=" "
)
# and then use this url as a link within a public login view
spotify_auth.authorize_url(
scope=["user-read-recently-played", "user-top-read"],
response_type="code"
)
# and have an authorize route that can direct you to Spotify or handle you being redirected back here from Spotify
@app.route('/authorize')
def authorize():
error = request.args.get("error")
if error:
abort(404)
code = request.args.get("code")
if code:
data = spotify_auth.get_token(
code=code,
grant_type="authorization_code",
)
access_token = data["access_token"]
refresh_token = data["refresh_token"]
scopes = data["scope"].split()
spotify_client = spotipy.Spotify(auth=access_token)
else:
return redirect(spotify_auth.authorize_url(
scope=["user-read-recently-played", "user-top-read"],
response_type="code",
state=session['state']
))
在第一次访问 时/authorize
,它会将用户重定向到 Spotify 的登录页面。但是当用户在那里成功登录时,它会将用户重定向回 Flask 站点(返回/authorize
),而不是执行prompt_for_user_token()
.
一旦它们重新打开/authorize
,这次有一个code
请求参数 - 因此它会发出 OAuth2 客户端到提供者请求来将此代码转换为您的访问令牌。
这两者都prompt_for_user_token
遵循相同的方法:
获取 Spotify 授权 URL 以引导用户登录
处理来自 Spotify 登录的重定向 - Flask 通过接收来自 Spotify 重定向到的 URL 的输入来
prompt_for_user_token
执行此操作,并通过让您从不存在的网页复制/粘贴 URL 来执行此操作。调用以将代码转换为用户的访问和刷新令牌。
我可能对这段代码做了一些修改,因为它是从我的 Spotify 集成的旧 Flask 实现中精心挑选出来的。这是一个更完整的要点,但我很确定很多 SQLAlchemy 的东西,授权视图,非常糟糕,所以请加一点盐。

TA贡献2051条经验 获得超10个赞
您可以尝试修改 util 模块。用 response = raw_input() 将 try-except 块替换为:
response = sys.stdin.readline().strip()
如果尚未导入,请不要忘记导入 sys。
这应该允许正常的 PIPE-ing。
或者您可以使用 pexpect 库而不是 subprocess 模块。它知道如何处理调整终端 fcntl 标志或在 Windows 上使用 mscvrt 的输入。
此外,当 PIPE-ing 数据时,不要忘记 raw_input()、input() 或 sys.stdin.readline() 在它们接收到适当的行尾字符之前不会返回。"\r"、"\n" 或 "\r\n"。您之前是否使用授权 URL 发送过?
添加回答
举报