Google Colab - Spotipy 没有将我重定向到指定的redirect_uri

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 笔记本上运行的代码

\n
import 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\'])\n
Run 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)

这样笔记本就会打印出你可以手动打开的网址: 在此输入图像描述