如何在无头设备上将 oAuth 与 spotipy 一起使用?

How do I use oAuth with spotipy on headless device?

我想创建一个简单的应用程序来跟踪我每天在 Spotify 上听音乐的时间。据我所知,我需要使用 oAuth 来接收此数据(用户阅读当前播放)。我目前正在这样验证:

token = util.prompt_for_user_token("<username>", scope, client_id="<clientID>", client_secret="<client_secret>", redirect_uri="http://localhost:4466")
spotify = spotipy.Spotify(auth=token)

我知道它有效,因为如果我 运行 它在我有图形显示的 Windows 上,一切都很好,我可以获得重定向 Url 和授权来自打开登录页面的浏览器的代码。有没有办法在无头 linux 机器上获取此代码?

我找到了解决这个问题的方法: 而不是使用

token = util.prompt_for_user_token("<username>", scope, client_id="<clientID>", client_secret="<client_secret>", redirect_uri="http://localhost:4466")
spotify = spotipy.Spotify(auth=token)

您可以使用

spotify = spotipy.Spotify(auth_manager=SpotifyOAuth(
    client_id="id", client_secret="secret", redirect_uri="http://localhost:4466", scope=scope, open_browser=False, ))

这实现了相同的目标。要在无头环境中进行身份验证,您可以将兑现的 Auth 从 windows 机器复制到您的 Linux 机器,只要它们都在打开 cmd 的文件夹中。