【问题标题】:getting “Only valid bearer authentication supported” when searching for artist info搜索艺术家信息时获得“仅支持有效的承载身份验证”
【发布时间】:2020-03-04 16:37:05
【问题描述】:

我正在尝试使用 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}}

【问题讨论】:

  • 我觉得Authorization一定是这样的:"Authorization": 'Bearer ' + token
  • 是的。谢谢它现在正在工作。
  • 太棒了。我将其添加为答案

标签: python python-3.x spotify


【解决方案1】:

您需要将“Bearer”附加到授权标头的值:

"Authorization": 'Bearer ' + token

编辑:

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

"Authorization": f'Bearer {token}'

【讨论】:

  • 既然是 python3 不是 "Authorization":f'Bearer {token}' 更好吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多