Android YouTube API edit

Consuming YouTube Data API on Android

This example will guide you how to get playlist data using the YouTube Data API on Android.

SHA-1 fingerprint

First you need to get an SHA-1 fingerprint for your machine. There are various methods for retrieving it. You can choose any method provided in this Q&A.

Google API console and YouTube key for Android

Now that you have an SHA-1 fingerprint, open the Google API console and create a project. Go to this page and create a project using that SHA-1 key and enable the YouTube Data API. Now you will get a key. This key will be used to send requests from Android and fetch data.

Gradle part

You will have to add the following lines to your Gradle file for the YouTube Data API:

compile ''

In order to use YouTube’s native client to send requests, we have to add the following lines in Gradle:

compile ''
compile ''
compile ''

The following configuration also needs to be added in Gradle in order to avoid conflicts:

configurations.all {
    resolutionStrategy.force ''

Below it is shown how the would finally look like.


apply plugin: ''
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.aam.skillschool"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner ""
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''
    configurations.all {
        resolutionStrategy.force ''

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('', {
        exclude group: '', module: 'support-annotations'
    compile ''
    compile ''
    compile ''
    compile ''
    compile ''
    compile ''

Now comes the Java part. Since we will be using HttpTransport for networking and GsonFactory for converting JSON into POJO, we don’t need any other library to send any requests.

Now I want to show how to get playlists via the YouTube API by providing the playlist IDs. For this task I will use AsyncTask. To understand how we request parameters and to understand the flow, please take a look at the YouTube Data API.

public class GetPlaylistDataAsyncTask extends AsyncTask<String[], Void, PlaylistListResponse> {
    private static final String YOUTUBE_PLAYLIST_PART = "snippet";
    private static final String YOUTUBE_PLAYLIST_FIELDS = "items(id,snippet(title))";

    private YouTube mYouTubeDataApi;

    public GetPlaylistDataAsyncTask(YouTube api) {
        mYouTubeDataApi = api;

    protected PlaylistListResponse doInBackground(String[]... params) {

        final String[] playlistIds = params[0];

        PlaylistListResponse playlistListResponse;
        try {
            playlistListResponse = mYouTubeDataApi.playlists()
                    .setId(TextUtils.join(",", playlistIds))
                    .setKey(AppConstants.YOUTUBE_KEY) //Here you will have to provide the keys
        } catch (IOException e) {
            return null;

        return playlistListResponse;

The above asynchronous task will return an instance of PlaylistListResponse which is a build-in class of the YouTube SDK. It has all the required fields, so we don’t have to create POJOs ourself.

Finally, in our MainActivity we will have to do the following:

public class MainActivity extends AppCompatActivity {
    private YouTube mYoutubeDataApi;
    private final GsonFactory mJsonFactory = new GsonFactory();
    private final HttpTransport mTransport = AndroidHttp.newCompatibleTransport();
    protected void onCreate(Bundle savedInstanceState) {
        mYoutubeDataApi = new YouTube.Builder(mTransport, mJsonFactory, null)
        String[] ids = {"some playlists ids here seperated by "," };
        new GetPlaylistDataAsyncTask(mYoutubeDataApi) {
            ProgressDialog progressDialog = new ProgressDialog(getActivity());

            protected void onPreExecute() {
                progressDialog.setTitle("Please wait.....");

            protected void onPostExecute(PlaylistListResponse playlistListResponse) {
                //Here we get the playlist data
                Log.d(TAG, playlistListResponse.toString());

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
186 YouTube API
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close