Skip to content

Authentication

RubenJ01 edited this page Jan 18, 2026 · 2 revisions

Authentication

Some endpoints need authentication (like track.scrobble). Here's how to set it up:

Step 1: Get a Token

First, create a client with your API key and secret, then request a token:

String apiKey = "your-api-key";
String apiSecret = "your-api-secret";

LastFmClient client = LastFmClient.create(apiKey, apiSecret);
String token = client.auth().getToken();

Step 2: Authorize the Token

Generate the authorization URL and visit it in your browser to grant permission:

String authUrl = client.auth().getAuthorizationUrl(apiKey, token);
System.out.println("Visit this URL to authorize: " + authUrl);

After granting permission, you'll see a success page. Tokens expire after 60 minutes and can only be used once.

Step 3: Get a Session Key

Once the token is authorized, exchange it for a session key:

Session session = client.auth().getSession(token);
String sessionKey = session.key();

Session keys don't expire, so save yours and reuse it for all future requests. You only need to do the token thing once.

Using authenticated endpoints

Now you can create an authenticated client:

LastFmClient authenticatedClient = LastFmClient.createAuthenticated(
    apiKey, 
    apiSecret, 
    sessionKey
);

This client can now use authenticated endpoints like track.scrobble, track.love, etc.

Clone this wiki locally