【问题标题】:currently_playing not working when using the Spotipy library使用 Spotipy 库时,current_playing 不起作用
【发布时间】:2018-04-07 21:21:45
【问题描述】:

我正在尝试使用 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'。我已经探索过使用 node,但我真的很想坚持使用 python。

【问题讨论】:

    标签: python api spotify spotipy


    【解决方案1】:

    您正在尝试使用尚未包含在最新发布版本中的功能。

    您可以看到on PyPI 表示当前版本于2017 年1 月5 日发布,但您可以看到on Github 表示您要调用的函数是在2017 年5 月13 日添加的。

    Issue #270Issue #211 都在询问何时将新版本推送到 PyPI,(此外,版本编号存在一些奇怪的问题。)

    无论如何,正如 #211 所说:

    如果您仍然遇到问题,您可以直接从 github repo 安装包。

    pip install git+https://github.com/plamere/spotipy.git --upgrade
    

    【讨论】:

    • 它适用于 python 2,但不适用于 python 3。我目前正在尝试使用 pip3 并找到解决方案,说明它为什么不起作用。
    【解决方案2】:

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

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

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

    请注意,您应该在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)
    

    【讨论】:

      猜你喜欢
      • 2022-06-18
      • 1970-01-01
      • 2019-12-18
      • 2019-11-09
      • 2019-04-12
      • 2015-07-26
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      相关资源
      最近更新 更多