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

Google Colab - Spotipy not redirecting me to the specified redirect_uri

我想使用 Spotipy python 包试用 Spotify API。

首先,在我的 Spotify Developer 应用中, 我已将 redirect_uri 设置为 https://example.com/callback/

这是我尝试在 Google Colab 笔记本

上 运行 的代码
import pandas as pd
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

spotify_details = {
    'client_id' : '<hidden>',
    'client_secret':'<hidden>',
    'redirect_uri':'https://example.com/callback/'}

scope = "user-library-read user-follow-read user-top-read playlist-read-private" 

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))

results = sp.current_user_saved_tracks()
for idx, item in enumerate(results['items']):
    track = item['track']
    print(idx, track['artists'][0]['name'], " – ", track['name'])

这是我看到的输出:

但是我的浏览器没有将我重定向到任何 URL 以便我接收身份验证令牌

我做错了什么?

Colab 是一个无浏览器环境,所以按照建议in the FAQ你应该添加参数

open_browser=False

当您实例化 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))

这样,notebook会打印出可以手动打开的URL: