Last.fm api invalid method

╄→尐↘猪︶ㄣ 提交于 2019-12-07 21:50:39

问题


I am trying to write a python script to do a query to Last.fm, but I keep getting an invalid method error returned.

I don't want links to pre-written last.fm python libraries, I am trying to do this as a "test what I know" kind of project. Thanks in advance!

import urllib
import httplib

params = urllib.urlencode({'method' : 'artist.getsimilar',
               'artist' : 'band',
               'limit' : '5',
               'api_key' : #API key goes here})

header = {"user-agent" : "myapp/1.0"}

lastfm = httplib.HTTPConnection("ws.audioscrobbler.com")

lastfm.request("POST","/2.0/?",params,header)

response = lastfm.getresponse()
print response.read()

回答1:


You lack Content-type for your request: "application/x-www-form-urlencoded". This works:

import urllib
import httplib

params = urllib.urlencode({'method' : 'artist.getsimilar',
               'artist' : 'band',
               'limit' : '5',
               'api_key' : '#API key goes here'})

header = {"user-agent" : "myapp/1.0",
          "Content-type": "application/x-www-form-urlencoded"}

lastfm = httplib.HTTPConnection("ws.audioscrobbler.com")

lastfm.request("POST","/2.0/?",params,header)

response = lastfm.getresponse()
print response.read()



回答2:


the Last.fm artist.getSimilar API method does not require POST, it can be done with a GET.

Only API methods that change data require the POST method.



来源:https://stackoverflow.com/questions/4362900/last-fm-api-invalid-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!