簡體   English   中英

Musixmatch API響應返回200,但是返回None值的json

[英]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. 就拿MusixMatch服務的swagger.jsongithub上

  2. 使用Swagger編輯器基於swagger.json生成python-client

  3. 安裝自動生成的模塊swagger-client

  4. 運行代碼

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}}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM