Spotify API播放接口返回Episode类型item为None的问题咨询
解决Spotify播放播客时
item字段为空的问题 你遇到的核心问题是调用/v1/me/player接口时,默认不会返回播客剧集(episode)的item数据,必须显式指定要获取的内容类型。
解决步骤
修改请求URL,添加
additional_types=episode参数
在请求播放状态时,需要告诉Spotify接口同时返回播客剧集的信息,把原来的请求地址改成:response = requests.get("https://api.spotify.com/v1/me/player?additional_types=episode", headers=headers)该参数支持多类型组合,比如同时需要歌曲和剧集数据可写
additional_types=track,episode,默认已包含歌曲类型,单独追加episode即可。确认权限配置
确保你的Spotify开发者应用已申请user-read-playback-state权限,缺少该权限会导致接口返回权限错误。
修改后的代码示例
调整后核心逻辑可简化,因为播放播客时item字段会直接返回完整数据:
def get_current_track(): if time.time() > globals.token_expiry: print("Access token expired, refreshing...") refresh_access_token() if not globals.access_token: print("No valid access token available after refresh.") return None, None, None, 0, 0 headers = {"Authorization": f"Bearer {globals.access_token}"} # 添加additional_types参数获取播客剧集数据 response = requests.get("https://api.spotify.com/v1/me/player?additional_types=episode", headers=headers) try: response_json = response.json() print("JSON Content: -------- ", response_json) except ValueError: print("No JSON content found in the response.") if response.status_code == 200: playback_info = response.json() if playback_info and playback_info['is_playing']: currently_playing_type = playback_info.get('currently_playing_type') item = playback_info.get('item') if currently_playing_type == 'track' and item: track_id = item['id'] track_name = item['name'] artist_name = item['artists'][0]['name'] progress_ms = playback_info['progress_ms'] duration_ms = item['duration_ms'] return track_id, track_name, artist_name, progress_ms, duration_ms elif currently_playing_type == 'episode' and item: # 直接从item提取剧集信息,无需额外请求 episode_id = item['id'] track_name = item['name'] artist_name = item['show']['publisher'] progress_ms = playback_info['progress_ms'] duration_ms = item['duration_ms'] return episode_id, track_name, artist_name, progress_ms, duration_ms else: print("No valid item data found for current playback.") else: print("No track or episode is currently playing.") else: print("Failed to get playback information. Response:", response.text) return None, None, None, 0, 0
说明
添加additional_types=episode参数后,接口返回的item字段会包含播客剧集的完整元数据,包括ID、标题、所属播客信息等,无需再通过context解析或额外调用剧集详情接口,直接从item中提取所需数据即可。
内容的提问来源于stack exchange,提问作者olavSR
相关产品推荐
相关产品推荐

