python - 使 Spotipy 程序变得用户友好

标签 python spotipy

我使用 Spotipy 创建了一个简单的 Python 程序,该程序根据用户设备中下载的轨道显示一些推荐的轨道。但我在使程序变得用户友好方面遇到了一些麻烦。

首先,通过将我的代码上传到 GitHub 等方式与用户共享我的客户端 ID 和客户端 key 是否有任何问题?我可以使用重定向 URI 作为 http://localhost/或者我应该为我的程序创建一个网站以确保安全?在用户名字段中,它应该是要分析的帐户的用户名,或者可以是任何内容,例如“Brian Rogers”

在身份验证部分,它在 Python 控制台中向用户显示以下消息:

User authentication requires interaction with your
    web browser. Once you enter your credentials and
    give authorization, you will be redirected to
    a url.  Paste that url you were directed to to
    complete the authorization.

Opening https://... in your browser

Enter the URL you were redirected to: 

我的问题是:既然我正在使用 Tkinter,如何将输入从 Tkinter 输入框重定向到 Python 控制台?

最后,身份验证 token 需要多长时间才会过期?如果是这样,如何更新它(如果可能的话,这样只有用户第一次运行程序时才进入)?

提前感谢患者!

最佳答案

我会一一解答您的所有问题。

is there any problem by sharing my Client ID and my Client Secret with the user by, for example, uploading my code in GitHub?

应始终避免将个人凭据放入源中。如果有人滥用您的凭据,您将承担责任,因为它们是您的凭据。无论如何,我能想象到的唯一可能造成的破坏就是向 Spotify 的 API 发送垃圾邮件请求,我相信 Spotify 的 API 已经有了保护措施,如果检测到垃圾请求,将会丢弃进一步的请求。我已经看到一些项目通过创建特殊帐户来为其项目生成 API 凭据,将其 Spotify 和 YouTube API 凭据放入其源代码中,并推送到 GitHub,以使该工具更易于设置和使用。

Can I use Redirect URI as being http://localhost/ or should I create a website for my program for securing purposes? In Username field, it should be the username of the account to be analyzed or it can be anything, like "Brian Rogers"?

由于您只是在 Spotify 上搜索相关轨道,因此我相信您可能不需要访问您正在使用其凭据的 Spotify 用户的个人信息。如果是这样,您可以使用 oauth2.SpotifyClientCredentials 对自己进行授权,从而避免传递用户名和验证重定向 URI:

import spotipy
import spotipy.oauth2 as oauth2

credentials = oauth2.SpotifyClientCredentials(
        client_id=client_id,
        client_secret=client_secret)

token = credentials.get_access_token()
# This won't prompt for verification of Redirect URI
sp = spotipy.Spotify(auth=token)

My question is: since I'm managing to use Tkinter, how can I redirect the input from the Tkinter input box to the Python console?

如果您使用上面提到的 oauth2.SpotifyClientCredentials,则不需要。

Finally, how long does the authentication token take to expire? And if so, how to renew it (if possible, so that only the user enters when they run the program for the first time)?

截至撰写本文时, token 的有效期恰好为一小时。您可以通过检查 credentials.token_info["expires_in"] 的值进行确认,该值显示时间(以秒为单位)。

此外,当调用依赖方法但 token 已过期时,spotipy 会引发 spotipy.client.SpotifyException。因此,您可以捕获此异常并用新实例覆盖之前的 spotipy.client.Spotify 实例。至少你会做类似的事情:

import spotipy
import spotipy.oauth2 as oauth2

def authenticate_calls():
    credentials = oauth2.SpotifyClientCredentials(
        client_id=client_id,
        client_secret=client_secret,
    )
    token = credentials.get_access_token()
    sp = spotipy.Spotify(auth=token)
    return sp

sp = authenticate_calls()

try:
    do_something_that_needs_authentication(sp)
except spotipy.client.SpotifyException:
    sp = authenticate_calls()
    do_something_that_needs_authentication(sp)

您还可以创建一个装饰器函数,如果 token 过期,它将刷新 token 并用它装饰您的函数!

关于python - 使 Spotipy 程序变得用户友好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56674952/

相关文章:

python - Spotipy:如何从播放列表中读取超过 100 首轨道

python - Pipenv:即使我安装了软件包,为什么会在自动创建锁定文件时运行 Pipenv Lock

python - 禁用 WTForms SelectField 中的选项之一

python - 使用 Spotipy 将轨道添加到 Spotify 中的播放列表

python-2.7 - 使用 Spotipy 设置 Spotify 凭据

python - Spotipy:readthedocs 中的简单代码出现异常

python - 如何通过spotipy检索收听历史对象?

python - 如何使用 matplotlib 创建堆叠折线图?

python - 大学类(class)高效排类

Python 四舍五入到自定义步骤