yvasyliev / deezer-api

Deezer API Java Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maven Central javadoc MIT License Build status CodeQL Technical Debt Quality Gate Status Reliability Rating Duplicated Lines (%) Vulnerabilities Bugs Security Rating Maintainability Rating Code Smells Lines of Code PRs Welcome

Deezer API Java Library

A Java implementation of Deezer API.

Third-party dependencies

  • SLF4J
  • Gson
  • OkHttp

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>2.1.2</version>
    </dependency>
  2. Use DeezerApi instance to 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

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:
         *          http://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.
        DeezerAccessToken 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

License:MIT License


Languages

Language:Java 100.0%