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

时间:2019-06-18 11:34:17

标签: python spotify spotipy

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

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: 

然后我必须将链接从浏览器复制到该空间。我可以使用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响应传递到输入提示

任何建议都将受到欢迎。

1 个答案:

答案 0 :(得分:0)

因此,有一个解决方法。您可以在本地计算机上运行一次,然后生成一个名为.cache-USERNAME的文件。如果您将该文件包含在部署包中,则无需复制/粘贴URL,并且可以使用AWS中的lambda函数将其自动化。