【问题标题】:Shazam detect API throws a HTTP 406 Not AcceptableShazam 检测 API 引发 HTTP 406 Not Acceptable
【发布时间】:2020-09-25 15:04:14
【问题描述】:

我正在尝试在this tutorial 提供的单声道样本上测试 Shazam API 检测功能。

我的代码读取原始音频文件并将其作为 base64 纯文本在 POST 请求的正文中发送到 Shazam API。

音频文件可以通过this link下载:

import requests
import base64

def shazam(payload):
    url = "https://shazam.p.rapidapi.com/songs/detect"

    payload = open(payload,"rb").read()
    payload = base64.b64encode(payload)
    payload = str(payload)
    headers = {
    'x-rapidapi-host': "shazam.p.rapidapi.com",
    'x-rapidapi-key':str(open("./api.txt","r").read().strip()),
    'content-type': "text/plain",
    'accept': "text/plain"
    }

    response = requests.request("POST", url, data=payload, headers=headers)

    print(response.text)

shazam("/home/samples/mono.raw")

任何想法我哪里出错了?

【问题讨论】:

  • 它在哪里崩溃?也许您可以编辑问题并添加崩溃日志。

标签: python api rest python-requests


【解决方案1】:

我在使用这个 API 时遇到了问题,并且一直在上下搜索以找到解决方案:(

【讨论】:

  • 我最终使用了另一个名为 AudD 的 API。强烈推荐。
【解决方案2】:

它对我有用

from pydub import AudioSegment
import base64
import requests
import json


file_path="./test.raw"
url = "https://rapidapi.p.rapidapi.com/songs/detect"
encode_string = base64.b64encode(open(file_path, "rb").read())
payload=encode_string
print(type(payload))

headers = {
    'content-type': "text/plain",
    'x-rapidapi-key': "<<<you key>>>",
    'x-rapidapi-host': "shazam.p.rapidapi.com"
    }

response = requests.request("POST", url, data=payload, headers=headers)
 
print(json.dumps(json.loads(response.text)))

【讨论】:

  • 欢迎来到 StackOverflow。虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
猜你喜欢
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 2019-12-15
  • 2021-01-21
  • 2018-01-08
  • 2018-07-31
  • 2013-08-10
相关资源
最近更新 更多