1.1. Authentication

Apart from your last.fm api key and secret you also need to be authenticated if you wish to perform write operations like scrobbling, adding/removing tags.

1.1.1. Mobile Applications

This is what Last.fm calls simple authentication with credentials, it’s indented to be used with non-browser application.

Last.fm states that Session keys have an infinite lifetime by default, so generate one and keep using it.

>>> from pydrag import configure, Album, Config, AuthSession
>>>
>>> configurate(api_key='aaaaaaaaaa', api_secret='bbbbbbbbbbb', username='foo', password='bar')
>>>
>>>
>>> session = AuthSession.authenticate()
>>> session.key
'2Y5jNDz1111111111111Zq3ZTNjl'
>>>
>>>
>>> configure(api_key='aaaaaaaaaa', api_secret='bbbbbbbbbbb', session='2Y5jNDz1111111111111Zq3ZTNjl')
>>> album = Album.find_by_mbid("6defd963-fe91-4550-b18e-82c685603c2b")
>>> album.add_tags(['great', 'rock'])
>>> Config.instance().session  # store this somewhere and next time
>>> configurate(api_key='aaaaaaaaaa', api_secret='bbbbbbbbbbb', session='ssssss')

1.1.2. Desktop Application

This method is indented for desktop applications, whatever that means.

Step 1: Generate an unauthorized token

1@app.route("/gen-token")
2def gen_token():
3    global tokens
4
5    token = AuthToken.generate()
6    tokens.update({token.token: None})
7    return redirect(url_for("index"))

Step 2: Send the user to the last.fm site to authorize the token

1@app.route("/authorize/<token>", methods=["GET"])
2def authorize_token(token):
3    global tokens
4    tokens[token] = True
5    token = AuthToken(token=token)
6    return redirect(token.auth_url)

Step 3: Retrieve a session with the authorized token

1@app.route("/get-session/<token>", methods=["GET"])
2def get_session(token):
3    global tokens
4    session = AuthSession.from_token(token)
5    tokens[token] = session
6    return redirect(url_for("index"))

1.1.3. Web Application

This method is very similar to the above but a lot simpler and makes more sense to me!

Step 1: Send the user to the last.fm site to authorize your application and provide a callback url which will include the authorized token for you!

 1@app.route("/session", methods=["POST", "GET"])
 2def gen_session():
 3    if request.method == "POST":
 4        url = "http://www.last.fm/api/auth/?api_key={}&cb={}".format(
 5            Config.instance().api_key, url_for("gen_session", _external=True)
 6        )
 7        return redirect(url)
 8    else:
 9        token = request.args.get("token")
10        return redirect(url_for("get_session", token=token))

Step 3: Retrieve a session with the authorized token

1@app.route("/get-session/<token>", methods=["GET"])
2def get_session(token):
3    global tokens
4    session = AuthSession.from_token(token)
5    tokens[token] = session
6    return redirect(url_for("index"))

Note

You can find the full demo application built with flask Here

$ pip install -r docs/examples/web/requirements.txt
$ FLASK_APP=docs/examples/web/app.py FLASK_DEBUG=1 python -m flask run