【问题标题】:How to auth in spotipy without open web browser?如何在不打开网络浏览器的情况下在 spotipy 中进行身份验证?
【发布时间】:2021-04-26 14:33:22
【问题描述】:

我尝试了标准方法(在主页上有描述),但我需要在没有网络浏览器的情况下登录。我该怎么做?

【问题讨论】:

  • 不确定我是否理解您的问题,但您可能想看看他们在 spotifyd(开源 spotify 客户端守护程序)中做了什么
  • 谢谢。我去看看

标签: python spotify spotipy


【解决方案1】:

您可以使用 Spotify 的 python 库之一,例如:https://github.com/plamere/spotipy

在 repo 中,您可以看到一个带有用户身份验证的示例

import spotipy
from spotipy.oauth2 import SpotifyOAuth

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="YOUR_APP_CLIENT_ID",
                                               client_secret="YOUR_APP_CLIENT_SECRET",
                                               redirect_uri="YOUR_APP_REDIRECT_URI",
                                               scope="user-library-read"))

results = sp.current_user_saved_tracks()
for idx, item in enumerate(results['items']):
    track = item['track']
    print(idx, track['artists'][0]['name'], " – ", track['name'])

(需要先安装库:pip install spotipy

另一个python库https://tekore.readthedocs.io/en/stable/

import tekore as tk

conf = (client_id, client_secret, redirect_uri)
token = tk.prompt_for_user_token(*conf, scope=tk.scope.every)

spotify = tk.Spotify(token)
tracks = spotify.current_user_top_tracks(limit=10)
spotify.playback_start_tracks([t.id for t in tracks.items])

【讨论】:

  • 根据标题OP使用spotipy...这说明你的答案很好,只是第一句话是没有必要的...
  • 我也使用这个库。问题是你第一次启动程序时,它会打开一个网络浏览器来登录。而我只需要登录而不打开网络浏览器
猜你喜欢
  • 2018-05-10
  • 1970-01-01
  • 2018-10-04
  • 2016-07-13
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
相关资源
最近更新 更多