我正在为 Spotify API 使用 python 包装器。
https://spotipy.readthedocs.io/en/latest/#installation
作为授权过程的一部分,Spotify API 让用户在其默认 Web 浏览器中登录 Spotify,然后将它们发送到预定义的(当您向 Spotify 注册应用程序时)REDIRECT_URI。此 REDIRECT_URI 当前设置为 localhost:6969。登录后,它会生成一个 input()(死亡输入),供您将重定向到的 URI 复制粘贴到命令提示符中。
我真的不想在使用命令提示符时向随机的人出售。
目标是在 localhost:6969 (起诉我)上打开一个烧瓶服务器,然后双击它的 /authorize 页面(发送到 oauth2 登录然后捕获代码)
http://flask.pocoo.org/docs/1.0/
所以 -
我在 jflask.py 中保留我的 Flask 装备和 Spotify 捕手:
from flask import Flask, request, redirect, session
from requests_oauth2 import OAuth2
from spotipy import Spotify
import spotipy.util as util
import requests
import datetime
app = Flask(__name__)
spotify_auth = OAuth2(
client_id='e33c6fa0d6a249ccafa232a9cf62a616',
client_secret='a43b0b6a4de14b97b4e468459e0f7824',
redirect_uri="http://localhost:6969/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
print(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():
print('triggered')
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()
self.sp = spotipy.Spotify(auth=access_token)
print(access_token)
return self.sp
我已经尝试将 Popen 与 stdout=PIPE 一起使用。经过进一步的研究,我很确定这也阻碍了我的成功。该规范旨在与 subprocess.communicate() 一起使用
https://docs.python.org/3/library/subprocess.html#subprocess.PIP
感谢大家的帮助。
慕田峪9158850
侃侃无极
相关分类