Skip to content

alecigne/deezer-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maven Central Build status MIT License PRs Welcome javadoc

Deezer API Java Library

A Java implementation of Deezer API.

Third-party dependencies

  • SLF4J
  • Gson

Requirements

  1. Java 8+
  2. Maven (or other build tool)

Quickstart

  1. Add to your pom.xml:
<dependency>
    <groupId>com.github.yvasyliev</groupId>
    <artifactId>deezer-api</artifactId>
    <version>1.0.6</version>
</dependency>
  1. Create and execute Deezer API requests:
public class DeezerApp {
    public static void main(String[] args) throws DeezerException {
        DeezerApi deezerApi = new DeezerApi();

        Album album = deezerApi.album().getById(302127).execute();
        System.out.println(album);

        TrackData trackData = deezerApi.search().searchTrack("eminem").execute();
        System.out.println(trackData);
    }
}

OAuth

Some Deezer API requests should be executed with access_token param passed.
To obtain access_token our application must be registered and the user must be authorized.
To register new app:

  1. Go to https://developers.deezer.com/myapps
  2. Create new Application.

Now we are ready to authorize the user. Deezer uses OAuth 2.0 protocol for authentication and authorization.

Authorization flow example

import api.deezer.exceptions.DeezerException;

public class DeezerApp {
    /**
     * Can be found at https://developers.deezer.com/myapps
     */
    private static final int APP_ID = 123;

    /**
     * Can be found at https://developers.deezer.com/myapps
     */
    private static final String SECRET = "secret_string";

    /**
     * Your domain where user will be redirected to.
     */
    private static final String REDIRECT_URI = "your.domain.com";

    public static void main(String[] args) throws DeezerException {
        DeezerApi deezerApi = new DeezerApi();

        // Step 1. Create login URL.
        String loginUrl = deezerApi.auth().getLoginUrl(APP_ID, REDIRECT_URI, Permission.BASIC_ACCESS);
        System.out.println(loginUrl); // https://connect.deezer.com/oauth/auth.php?app_id=123&redirect_uri=your.domain.com&perms=basic_access

        /*
         * Step 2. Authorize.
         *
         *      1. Open loginUrl in your browser.
         *      2. Login to Deezer. (if haven't done yet)
         *      3. Accept application permissions.
         *      4. Find a 'code' parameter in URL after redirection to:
         *          https://redirect_uri?code=A_CODE_GENERATED_BY_DEEZER
         */
        System.out.print("Please enter code: ");
        String code = new Scanner(System.in).next();

        // Step 3. Get access_token.
        AccessToken accessToken = deezerApi.auth().getAccessToken(APP_ID, SECRET, code).execute();
        deezerApi.setAccessToken(accessToken);

        // Now we are ready to execute any request we want.
        User me = deezerApi.user().getMe().execute();
        System.out.println(me);

        TrackData favouriteTracks = deezerApi.user().getFavouriteTracks(me.getId()).execute();
        System.out.println(favouriteTracks);
    }
}

About

Deezer API Java Library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%