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

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

首先,在我的 Spotify 开发者应用中, 我已将 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'])

这是我看到的输出:

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

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

我做错了什么?

a65758872 回答:Google Colab - Spotipy 没有将我重定向到指定的 redirect_uri

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: enter image description here

本文链接:https://www.f2er.com/3851.html

大家都在问