首页 > 解决方案 > python 上的 Spotify API 授权错误

问题描述

我正在按照教程 创建 Spotify 播放列表。我已经遵循了每一步,但我遇到了授权问题。它应该返回一个包含歌曲的列表(稍后我会将它们输入到播放列表中),但我收到此错误:<class 'dict'>: {'error': {'status': 400, 'message': 'Only valid支持承载认证'}}

这是我的代码:

import requests

url = "https://api.spotify.com/v1/recommendations?"
token = "I WILL CREATE A TOKEN GETTER"

#Filtros
limit = input("How many songs? ")
market = "ES"
seed_genres = input("Select the genres: ")
target_energy = int(input("From 0 to 10 how energetic do you want your playlist? "))/10

#Contactar con Spotify
query = f"{url}limit={limit}&market={market}&seed_genres={seed_genres}&target_energy={target_energy}"

response = requests.get(query,
                       headers = {"Content-Type":"application/json",
                                  "Authorization": f'{token}'})
json_response = response.json()

for i in json_response['tracks']:
            uris.append(i)
            print(f"\"{i['name']}\" by {i['artists'][0]['name']}")

标签: pythonjsonapispotify

解决方案


您发送的令牌是什么?您是否仅将令牌存储在变量中token?确保您以以下形式提供令牌Authorization: Bearer <YOURTOKEN>。也许“承载者”这个词不见了?否则,您应该确保令牌实际上是有效的(请参阅文档)。


推荐阅读