Shr*_*pre 2 python spotify jupyter-notebook spotipy google-colaboratory
我想使用 Spotipy python 包来试验 Spotify API。
\n首先,在我的 Spotify Developer 应用程序中,\n我已将redirect_uri 设置为https://example.com/callback/
\n这是我尝试在 Google Colab 笔记本上运行的代码
\nimport pandas as pd\nimport spotipy\nfrom spotipy.oauth2 import SpotifyClientCredentials\n\nspotify_details = {\n \'client_id\' : \'<hidden>\',\n \'client_secret\':\'<hidden>\',\n \'redirect_uri\':\'https://example.com/callback/\'}\n\nscope = "user-library-read user-follow-read user-top-read playlist-read-private" \n\nsp = spotipy.Spotify(\n auth_manager=spotipy.SpotifyOAuth(\n client_id=spotify_details[\'client_id\'],\n client_secret=spotify_details[\'client_secret\'],\n redirect_uri=spotify_details[\'redirect_uri\'], \n scope=scope))\n\nresults = sp.current_user_saved_tracks()\nfor idx, item in enumerate(results[\'items\']):\n track = item[\'track\']\n print(idx, track[\'artists\'][0][\'name\'], " \xe2\x80\x93 ", track[\'name\'])\nRun Code Online (Sandbox Code Playgroud)\n这就是我所看到的输出:
\n\n但我的浏览器没有将我重定向到任何 URL 来接收身份验证令牌
\n我究竟做错了什么?
\n小智 5
Colab 是一个无浏览器环境,因此根据常见问题解答中的建议,您应该添加参数
open_browser=False
Run Code Online (Sandbox Code Playgroud)
当你实例化你的spotipy.SpotifyOAuth对象时:
sp = spotipy.Spotify(
auth_manager=spotipy.SpotifyOAuth(
client_id=spotify_details['client_id'],
client_secret=spotify_details['client_secret'],
redirect_uri=spotify_details['redirect_uri'],
scope=scope, open_browser=False))
Run Code Online (Sandbox Code Playgroud)