1

我正在尝试使用 spotipy Spotify python 库访问用户当前正在播放的音乐。

import json
import spotipy
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials 

cid = "xxx"
csecret = "xxx"
redirectURI = "xxx"
username = "xxx"

client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=csecret) 
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

scope = 'user-read-currently-playing'
token = util.prompt_for_user_token(username, scope, cid, csecret, redirectURI)

if token:
    sp = spotipy.Spotify(auth=token)
else:
    print("Can't get token for", username)

current_track = sp.current_user_playing_track()
print(json.dumps(current_track, sort_keys=False, indent=4))

我也尝试过使用sp.currently_playing(). 我能够访问其他数据,例如sp.current_user_saved_tracks(limit=3, offset=0). 使用我目前拥有的东西,它总是会出错 AttributeError: 'Spotify' object has no attribute 'current_user_playing_track'。我已经探索过使用节点,但我真的很想坚持使用 python。

4

2 回答 2

2

You're trying to use a feature that isn't in the latest released version yet.

You can see on PyPI that the current version was released on 5 Jan 2017, but you can see on Github that the function you want to call was added on 13 May 2017.

Issue #270 and Issue #211 are both asking when a new release will be pushed to PyPI, (Also, there's some weird issue with the version numbering.)

Anyway, as #211 says:

If you're running into issues still, you can install the package directly from the github repo.

pip install git+https://github.com/plamere/spotipy.git --upgrade
于 2018-04-07T21:59:47.843 回答
1

所以我也一直在尝试访问我当前的歌曲,在网络上每个错误答案之后的 3 天后......我正在使用Authorization Code Flow这里所说的https://spotipy.readthedocs.io/en/2.12.0/#client -凭据流

只能访问不访问用户信息的端点

由于我无法设置环境变量,因此我使用另一个脚本进行了设置。

请注意,您应该在https://developer.spotify.com/dashboard/applications/中添加重定向链接,例如“ https://localhost:8000/


这是我添加我的信用的代码:

import os

class SetCreditentials:

    def __init__(self):
        self.CLIENT_SECRET = os.getenv('SPOTIPY_CLIENT_SECRET')
        self.CLIENT_ID = os.getenv('SPOTIPY_CLIENT_ID')
        self.REDIRECT_URI = os.getenv('SPOTIPY_REDIRECT_URI')
        os.environ['SPOTIPY_CLIENT_ID'] = ""
        os.environ['SPOTIPY_CLIENT_SECRET'] = ""
        os.environ['SPOTIPY_REDIRECT_URI'] = "http://localhost:8000/"

这是访问任何数据的代码(此处为当前播放曲目):

import spotipy.util as util
import spotipy
import Creditentials

# Authorization Code Flow
user = "213tzif5o7rzyxtijuqdgtfuq"
scope = "user-read-currently-playing"

Creditentials.SetCreditentials()
token = util.prompt_for_user_token(user, scope)
track = spotipy.Spotify(token).current_user_playing_track()
print(track)
于 2020-05-27T16:26:34.497 回答