pythonrestlast.fm

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'

method = "track.scrobble"
artist = "Muse"
track = "Time Is Running Out"
timestamp = str(time.time())
api_sig = hashlib.md5(
f"api_key{API_KEY}artist{artist}method{method}sk{session_key}timestamp{timestamp}track{track}{API_SECRET}".encode('utf-8')).hexdigest()

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

# API URL
url = "https://ws.audioscrobbler.com/2.0/"

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

params['api_sig'] = api_sig


# Sending the POST request
try:
    response = requests.post(url, params=params, headers=header)
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.

Update: If I set the api sig to look like this:

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')

).hexdigest()

but this is returning Invalid session key - Please re-authenticate with a brand newly generated session key I know to be valid. If I'm not mistaken, those sessions don't expire.


Solution

  • You forgot to hexdigest your md5 signature in this line:

    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')
    )
    

    This should work:

    import time, requests, hashlib
    
    # API method and parameters
    API_KEY = 'API_KEY'
    session_key = 'session_key'
    API_SECRET = 'API_SECRET'
    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')
    ).hexdigest()
    # 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)
    
    print(response.text)
    

    Response:

    <?xml version="1.0" encoding="UTF-8"?>
    <lfm status="ok">
      <scrobbles ignored="0" accepted="1">
        <scrobble>
          <track corrected="0">Time Is Running Out</track>
          <artist corrected="0">Muse</artist>
          <album corrected="0" />
          <albumArtist corrected="0"></albumArtist>
          <timestamp>1692987267</timestamp>
          <ignoredMessage code="0"></ignoredMessage>
        </scrobble>
      </scrobbles>
    </lfm>