2010-12-06 7 views
2

Ich versuche, ein Python-Skript zu schreiben, um eine Abfrage zu Last.fm zu tun, aber ich bekomme immer eine ungültige Methode Fehler zurückgegeben.Last.fm api ungültige Methode

Ich möchte keine Links zu vor geschriebenen last.fm Python-Bibliotheken, ich versuche dies als ein "Test, was ich weiß" Art von Projekt zu tun. Danke im Voraus!

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() 
+0

Was ist der Fehler? – user225312

Antwort

2

Sie fehlen Content-type für Ihre Anfrage: "application/x-www-form-urlencoded". Dies funktioniert:

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() 
+0

Pfui, vielen Dank –

0

die Last.fm artist.getSimilar API-Methode erfordert keine POST, kann es mit einem GET erfolgen.

Nur API-Methoden, die Daten ändern, erfordern die POST-Methode.

Verwandte Themen