Python Spotify API 如何将变量从 util.prompt_for_user_token 响应传递到输入提示

Python Spotify API How to pass variable into the input prompt from the util.prompt_for_user_token response

我正在使用 spotify API 创建一个管道来记录我的流媒体历史记录。我计划通过将其作为 lambda 函数上传并每隔几个小时将其安排到 运行 来实现自动化。除了第一个 运行 和 API 需要网络身份验证外,我的一切都井井有条。这是我的代码:

import spotipy
import spotipy.util as util
import urllib3

un = USERNAME
scope = 'user-read-recently-played'
cid = CLIENT_ID
csid = CLIENT_SECRET_ID
redr = r'http://localhost:8888/callback/'

token = util.prompt_for_user_token(un,scope,cid,csid,redr)

这是第一次 运行 时,会弹出此消息:

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.


Opened <LINK HERE> in your browser


Enter the URL you were redirected to: 

然后我必须将 link 从我的浏览器复制到 space。我可以使用 urllib3:

获得我需要粘贴的 URL
req_adr = ADDRESS_IT_OPENS_IN_BROWSER
http = urllib3.PoolManager()
resp = http.request('GET',req_adr)
redrurl = resp.geturl()

但我不知道如何将它从 util.prompt_for_user_token 响应传递到输入提示

非常欢迎任何建议。

事实证明有一个解决方法。您可以在本地计算机上 运行 它一次,然后生成一个名为 .cache-USERNAME 的文件。如果您将该文件包含在部署包中,则不必 copy/paste URL 并且它可以通过 AWS 中的 lambda 函数自动执行。