Musixmatch api 响应返回 200 但返回 json 和 None 值

Musixmatch api response returning 200 but returning json with None values

我正在使用 MusixMatch API 获取给定 musixmatch 分配的专辑 ID 的曲目列表。我使用的相册 ID 是 20903197,我已经验证它有效。 https://playground.musixmatch.com/#!/Track/get_album_tracks_get 将其插入 album_id 字段

但是,当我尝试使用他们的 python sdk 调用它时,我收到了所有属性的 None 响应,但返回 200 响应。我在他们的网站上大摇大摆地测试了它,它工作正常。有没有可能他们的 api 坏了或者我做错了什么?

这是我当前的脚本:

import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint


swagger_client.configuration.api_key['apikey'] = 'API_KEY_HERE'



api_instance = swagger_client.TrackApi()
album_id = '20903197' # str | The musiXmatch album id
format = 'json' # str | output format: json, jsonp, xml. (optional) (default to json)
callback = 'callback_example' # str | jsonp callback (optional)
f_has_lyrics = 'f_has_lyrics_example' # str | When set, filter only contents with lyrics (optional)
page = 3.4 # float | Define the page number for paginated results (optional)
page_size = 3.4 # float | Define the page size for paginated results.Range is 1 to 100. (optional)

try:
    api_response = api_instance.album_tracks_get_get(album_id, format=format, callback=callback, f_has_lyrics=f_has_lyrics, page=page, page_size=page_size)
    pprint(api_response)
except ApiException as e:
    print "Exception when calling TrackApi->album_tracks_get_get: %s\n" % e

我收到了这样的回复:

{'message': {'body': {'track_list': [{'album_coverart_100x100': None,
                                      'album_coverart_350x350': None,
                                      'album_coverart_500x500': None,
                                      'album_coverart_800x800': None,
                                      'album_id': None,
                                      'album_name': None,
                                      'artist_id': None,
                                      'artist_mbid': None,
                                      'artist_name': None,
                                      'commontrack_id': None,
                                      'commontrack_vanity_id': None,
                                      'explicit': None,
                                      'first_release_date': None,
                                      'has_lyrics': None,
                                      'has_subtitles': None,
                                      'instrumental': None,
                                      'lyrics_id': None,
                                      'num_favourite': None,
                                      'primary_genres': None,
                                      'restricted': None,
                                      'secondary_genres': None,
                                      'subtitle_id': None,
                                      'track_edit_url': None,
                                      'track_id': None,
                                      'track_isrc': None,
                                      'track_length': None,
                                      'track_mbid': None,
                                      'track_name': None,
                                      'track_name_translation_list': None,
                                      'track_rating': None,
                                      'track_share_url': None,
                                      'track_soundcloud_id': None,
                                      'track_spotify_id': None,
                                      'track_xboxmusic_id': None,
                                      'updated_time': None}]},
             'header': {'available': 1.0,
                        'execute_time': 0.0039160251617432,
                        'status_code': 200.0}}}

要找到此问题的原因,需要查看 Swagger Editor 生成的 swagger-client 代码:对于同步请求,请查看 api_client.py -> __call_api,服务返回的结果默认被反序列化,导致负载为空。

要修复它需要通过设置参数 _preload_content=False.

来禁用反序列化

重现步骤:

  1. github

  2. 上使用 swagger.json MusixMatch 服务
  3. 使用Swagger Editor根据swagger.json

  4. 生成python-client
  5. 安装自动生成的模块swagger-client

  6. 运行代码

import swagger_client
from swagger_client.rest import ApiException
import json


configuration = swagger_client.Configuration()
configuration.api_key['apikey'] = '..'

api_instance = swagger_client.TrackApi(swagger_client.ApiClient(configuration))
album_id = 20903197

try:
    api_response = api_instance.album_tracks_get_get(album_id, _preload_content=False)
    result = json.loads(api_response.data)
    print(result)
except ApiException as e:
    print("Exception when calling TrackApi->album_tracks_get_get: %s\n" % e)

# result: {u'message': {u'body': {u'track_list': [{u'track': {u'track_share_url': u'https://www.musixmatch.com/lyrics/Kaskade-feat-Ilsey/Disarm-You?utm_source=application&utm_campaign=api&utm_medium=1982%3A1409612089803', u'album_name': u'Disarm You (feat. Ilsey)', u'has_subtitles': 1, u'track_name': u'Disarm You (feat. Ilsey)', u'primary_genres': {u'music_genre_list': [{u'music_genre': {u'music_genre_parent_id': 34, u'music_genre_name_extended': u'Dance', u'music_genre_vanity': u'Dance', u'music_genre_id': 17, u'music_genre_name': u'Dance'}}]}, u'album_id': 20903197, u'explicit': 0, u'has_lyrics': 1, u'artist_name': u'Kaskade feat. Ilsey', u'track_id': 84384445, u'instrumental': 0, u'updated_time': u'2015-10-16T17:38:45Z', u'track_rating': 21, u'commontrack_id': 46959082, u'restricted': 0, u'num_favourite': 773, u'artist_id': 28754430, u'track_name_translation_list': [], u'has_richsync': 1, u'track_edit_url': u'https://www.musixmatch.com/lyrics/Kaskade-feat-Ilsey/Disarm-You/edit?utm_source=application&utm_campaign=api&utm_medium=1982%3A1409612089803'}}]}, u'header': {u'available': 1, u'status_code': 200, u'execute_time': 0.0088551044464111}}}