-
Notifications
You must be signed in to change notification settings - Fork 0
Authentication
RubenJ01 edited this page Jan 18, 2026
·
2 revisions
Some endpoints need authentication (like track.scrobble). Here's how to set it up:
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();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.
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.
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.