搜索艺术家信息时出现“仅支持有效的持有者身份验证”

getting “Only valid bearer authentication supported” when searching for artist info

我正在尝试使用 Spotify API 获取艺术家信息。授权密钥适用于 Spotify console。但是当我 运行 我的机器中的 python 文件时,它显示错误,仅支持有效的承载身份验证。

import requests

import json
import pprint

endpoint_url = "https://api.spotify.com/v1/search?"

# OUR FILTERS
query = "john"
typ = "artist"
limit = 1
offset = 1
token="BQCjNYC2q_m1e_JAQgttZ3F-1CW1mnai9E2psfLbwY15kbtd7u2sjuPiF7ld8hbUshPJGLlBK3kLDIebgtk_K3bDrggExZDJ1zEMb8WVvV1DEx2rUK2youoeqT1rmXDT99P04rs8O0ie"

query = f'{endpoint_url}q={query}&type={typ}&limit={limit}&offset={offset}'

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

结果:

{'error': {'message': 'Only valid bearer authentication supported',
           'status': 400}}

您需要将 'Bearer' 附加到授权值 header:

"Authorization": 'Bearer ' + token

编辑:

当您使用 Python 3.6 或更高版本时:

"Authorization": f'Bearer {token}'