Friday 18 August 2023

How do I scrobble to Last.fm's API in Python without getting Invalid method signature supplied?

I have exchanged a token for a session key that looks like this "UserName 94ag345Sd3452C2ffa3aT 0"

I have the following code: session_key = 'UserName 94ag345Sd3452C2ffa3aT 0'

# API method and parameters
method = "track.scrobble"
artist = "Muse"
track = "Time Is Running Out"
timestamp = int(time.time())
api_sig = hashlib.md5(
    f"api_key{API_KEY}artist[0]{artist}method{method}sk{session_key}timestamp[0]{timestamp}track[0]{track}{API_SECRET}".encode('utf-8')
)
# API URL
url = "http://ws.audioscrobbler.com/2.0/"

# Parameters for the POST request
params = {
    "method": method,
    "api_key": API_KEY,
    "api_sig": api_sig,
    "sk": session_key,
    "artist[0]": artist,
    "track[0]": track,
    "timestamp[0]": timestamp,
}

# Sending the POST request
try:
    response = requests.post(url, data=params)
except requests.exceptions.RequestException as e:
    print(e)
    sys.exit(1)

return response.text

Why does it keep returning Invalid Method Signature? I have utf encoded my params. All required params are there. I've exchanged a token for a session key.



from How do I scrobble to Last.fm's API in Python without getting Invalid method signature supplied?

No comments:

Post a Comment