我正在试用我的mac 10.10预装的python 2.7.10,特别是[add_a_saved_track.py] [1]这是从github复制的代码:
# Add tracks to 'Your Collection' of saved tracks
import pprint
import sys
import spotipy
import spotipy.util as util
scope = 'user-library-modify'
if len(sys.argv) > 2:
username = sys.argv[1]
tids = sys.argv[2:]
else:
print("Usage: %s username track-id ..." % (sys.argv[0],))
sys.exit()
token = util.prompt_for_user_token(username, scope)
if token:
sp = spotipy.Spotify(auth=token)
sp.trace = False
results = sp.current_user_saved_tracks_add(tracks=tids)
pprint.pprint(results)
else:
print("Can't get token for", username)
Run Code Online (Sandbox Code Playgroud)
我使用developer.spotify.com/my-applications注册了该应用程序,并收到了client_id和client_secret.我对redirect_uri的选择有点不清楚所以我把它设置为' https://play.spotify.com/collection/songs '
从终端运行这个我得到一个错误,说:
You need to set your Spotify API credentials. You …
Run Code Online (Sandbox Code Playgroud) 我正在使用Spotipy python库与Spotify web api进行交互.我已经完成了API和文档,但是我没有看到一个明确的示例,它显示了库如何支持授权代码流(https://developer.spotify.com/web-api/authorization-guide/#authorization-code-流动).
我在 Django 中构建了一个应用程序,它使用 Spotipy(一个 Spotify API Python 库),并使用该spotipy.util.prompt_for_user_token()
命令来生成令牌并访问我的私人库:
import spotipy
import spotipy.util as util
import os, ast
#Spotify API keys
scope = "playlist-read-private"
uir = "http://localhost:8000"
username = "<MY_USERNAME>"
spotify_uid = os.environ["SPOTIFY_UID"]
spotify_usec = os.environ["SPOTIFY_USEC"]
print "retrieved keys from OS"
#set up access
def get_access():
try:
token = util.prompt_for_user_token(username, scope, spotify_uid, spotify_usec, uir)
print "SUCCESS"
return spotipy.Spotify(auth=token)
except:
print "FAILED TO LOAD"
Run Code Online (Sandbox Code Playgroud)
我希望该应用程序具有 ui 登录名而不是硬编码登录名,但我不知道如何进行。
目前我有一个“登录”按钮,它尝试通过 Javascript 触发登录页面重定向,方法是使用用户名参数调用上面的代码,但这会打开一个新页面,控制台中显示以下内容:
User authentication requires interaction with your
web browser. Once you …
Run Code Online (Sandbox Code Playgroud) 我正在使用spipy从Spotify检索一些曲目使用python。因此,我收到令牌过期错误,我想刷新我的令牌。但我不明白如何从 spipy 获取刷新令牌。
是否有另一种方法来刷新令牌或重新创建令牌?
谢谢你。
我有一个使用Spotipy的长时间运行的脚本。一个小时后(根据Spotify API),我的访问令牌失效。我已经成功地捕获了该令牌,但是在实际刷新令牌方面我不知道从那里可以走。我使用的是授权码流程,而不是客户端凭据。这是我的授权方式:
token = util.prompt_for_user_token(username,scope=scopes,client_id=client_id,client_secret=client_secret, redirect_uri=redirect_uri)
sp = spotipy.Spotify(auth=token)
Run Code Online (Sandbox Code Playgroud)
我见过的所有刷新示例都涉及一个oauth2
对象(例如oauth.refresh_access_token()
),并且docs仅列出该对象作为刷新令牌的方法。据我了解,通过授权代码流,您不需要一个oauth
对象(因为您使用进行了身份验证prompt_for_user_token()
)。如果是这种情况,我该如何刷新令牌?
当我尝试在 Spotipy 中运行 cretin 模块时,我收到客户端范围不足的错误消息。Spotify 给出 403 客户端错误,提示“未提供令牌”
尝试了多个模块,只有少数工作,例如 current_user() 模块,但其他模块却吐出客户端范围不足的错误。
尝试了多个模块,只有少数工作,例如 current_user() 模块,但其他模块却吐出客户端范围不足的错误。
import spotipy
import os
import json
import sys
import webbrowser
import spotipy.util as util
from json.decoder import JSONDecodeError
username = sys.argv[0]
try:
token = util.prompt_for_user_token(username)
except:
os.remove(f".cache-{username}")
token = util.prompt_for_user_token(username)
spotify = spotipy.Spotify(auth=token)
albums = spotify.current_user_saved_albums(limit= 100, offset = 0)
userplaylist = spotify.current_user_playlists()
spuser= spotify.current_user()
class spotifyUser:
def __init__(self, userid, followers, playlist):
self.userid = userid
self.followers = followers
self.playlist = playlist
self.albums
#list all user playlists …
Run Code Online (Sandbox Code Playgroud) 我目前正在研究从 Spotify 中的歌曲中提取特定项目。我想从艺术家那里获取具体的发行日期和流派。我正在从 pandas 数据报中提取信息,如下所示:。我尝试过手动完成大部分工作,但作为一个包含约 1100 首歌曲的数据帧,这变得很乏味。所以我研究了一些 API 来帮助解决这个问题,特别是 Spotipy。我有一个想法,我会从以下开始:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials #To access authorised Spotify data
client_id = 'client_id'
client_secret = 'client_secret'
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) #spotify object to access API
name = "AJR" #chosen artist
result = sp.search(name) #search query
result['tracks']['items'][0]['artists']
Run Code Online (Sandbox Code Playgroud)
但我不知道该去哪里。我尝试查看文档,虽然有关于如何从 Spotify For Developers 页面获取流派和日期的信息,但我似乎无法在 Spotipy 中找到它。任何关于从这里开始或如何实现算法以获得所需细节的建议都会很棒。谢谢。
我正在尝试使用其元数据创建我在 Spotify 上保存的所有曲目的数据集。我已经获得了所有歌曲功能、曲目名称和曲目 ID。我想添加一列曲目的艺术家和流派之一。
我尝试通过“liked_tracks.extend”添加它,但我无法让它工作。
cid =""
secret = ""
redirect_uri = 'http://localhost:8000/callback'
FEATURE_KEYS = ['danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo']
OFFSET=0
SAVED_TRACKS_LIMIT=50
FEATURE_LIMIT = 100
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=cid,
client_secret=secret,
redirect_uri=redirect_uri,
scope="user-library-read"))
liked_tracks = list()
print(liked_tracks)
while(True):
paged_tracks = sp.current_user_saved_tracks(offset=OFFSET, limit=SAVED_TRACKS_LIMIT)
liked_tracks.extend([{'name':el['track']['name'],
'id':el['track']['id']} for el in paged_tracks['items']])
print(f'Fetched {len(liked_tracks)} tracks')
OFFSET+=SAVED_TRACKS_LIMIT
if paged_tracks['next'] is None:
break
def get_windowed_track_ids(liked_tracks, limit):
for i in range(0, len(liked_tracks), limit):
track_window = liked_tracks[i:i + limit]
yield track_window, [t['id'] for t …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Spotipy 库来提取用户曲目并制作播放列表。每个函数单独工作(getTracks、makePlaylist);但是,它们需要不同的范围。
def generate_token(scope):
token = util.prompt_for_user_token(
username='al321rltkr20p7oftb0i801lk',
scope=('user-library-read','playlist-modify-private'),
client_id='0e7ea227ef7d407b8bf47a4c545adb3c',
client_secret='267e96c4713f46d4885a4ea6a099ead4',
redirect_uri='http://www.google.com')
return token
Run Code Online (Sandbox Code Playgroud)
这将返回错误“AttributeError: 'tuple' object has no attribute 'split'” 当我尝试将两个范围作为列表发送时,我也会收到错误。有想法该怎么解决这个吗?
我可能对Flask会话的工作原理不太了解,但我正在尝试使用带有授权代码流的SpotiPY生成 Spotify API 访问令牌,并将其存储在 Flask 的会话存储中。
该程序似乎无法存储它,因此稍后在尝试调用它时遇到错误。这是带有图像和标题的视觉解释:https : //imgur.com/a/KiYZFiQ
这是主要的服务器脚本:
from flask import Flask, render_template, redirect, request, session, make_response,session,redirect
import spotipy
import spotipy.util as util
from credentz import *
app = Flask(__name__)
app.secret_key = SSK
@app.route("/")
def verify():
session.clear()
session['toke'] = util.prompt_for_user_token("", scope='playlist-modify-private,playlist-modify-public,user-top-read', client_id=CLI_ID, client_secret=CLI_SEC, redirect_uri="http://127.0.0.1:5000/index")
return redirect("/index")
@app.route("/index")
def index():
return render_template("index.html")
@app.route("/go", methods=['POST'])
def go():
data=request.form
sp = spotipy.Spotify(auth=session['toke'])
response = sp.current_user_top_artists(limit=data['num_tracks'], time_range=data['time_range'])
return render_template("results.html",data=data)
if __name__ == "__main__":
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
这是两个 html 文件: …