Fetch data from YOUTUBE Data API v3 for Android developers or Any (Part 01)

fetch data from youtube data api - athaebula.com
Read Time:3 Minute, 27 Second

This article is for those who need to fetch data from YouTube Data API to build awesome web/native/cross applications and this is a summary from an enormous Google dev diary.

Before move further, you should have some idea of the below steps. This article writes on 16-04-2020 and some functionalities can be changed in the near future

  • About Google accounts
  • What is Google Cloud Platform known as GCP
  • How to get an API key from GCP
  • How to obtain authorization credentials
  • How to enable APIs from GCP
  • The implementation skills with JSON data format
tech-pro-athaebula.com

Below steps are essential for a move further, In this article, we move with a brief intro on how to fetch data from YouTube Data API and will discuss further more about this topic in feature using Real-life examples and Live recording of implement the example app.

Firstly let’s discuss about the features of this API.

Supported operations

Before check supported operation, below listed the operations that we can execute

Operations
listRetrieves (GET) a list of zero or more resources.
insertCreates (POST) a new resource.
updateModifies (PUT) an existing resource to reflect data in your request.
deleteRemoves (DELETE) a specific resource.

As you see above we can add multiple action for selected channel or our own channel

And here we have to consider about the quota usage of the API key. For each operation have its own quota usage.

As Ex:

  • A simple read operation that only retrieves the ID of each returned resource has a cost of approximately 1 unit.
  • A write operation has a cost of approximately 50 units.
  • A video upload has a cost of approximately 1600 units.

Let’s cut the crap and do the work guys…

I’ll demonstrate here very basic steps on how to fetch data from search endpoints in an Android app. Please follow the steps and if you have any question please comment

STEP 1 – in GCP console – YouTube Data API v3

  1. Enable YouTube Data API v3 Library
  2. Create an API key from the credential page
  3. Add correct info in OAuth consent screen

Now we configured our GCP properly. Then we need to create YouTube search endpoints.

STEP 2 – YouTube Data API v3

YouTube Data API v3 docs are described as the API that we going to use in this example. The search endpoints are not required 0Auth authentication from Google 0Auth, just need an API key. So the implementation is so simple.

To generate API endpoints that we want to go use YouTube Data API v3 search list docs. Using Try-It popup Dialog box, we can test and create endpoints easily.

The endpoint I created for testing is show as below

https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCrM1_toI5TsDYP1ZgeP91fw&key=[YOUR_API_KEY]'

STEP 3 – YouTube Data API v3

I’ll demonstrate how to use this API endpoint in Android application

To handle HTTP requests I use Volley library Add below dependencies into app level Gradle

implementation 'com.android.volley:volley:1.1.1'

STEP 3 – YouTube Data API v3

Using Volley, implement HTTP GET request using above params

RequestQueue queue = Volley.newRequestQueue(context);
            String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCrM1_toI5TsDYP1ZgeP91fw&key=[YOUR_API_KEY]";

            JsonObjectRequest objectRequest = new JsonObjectRequest(
                    Request.Method.GET,
                    url,
                    null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(final JSONObject res) {
                             log.d(TAG, "response: " + res)
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                        }
                    }
            );
            queue.add(objectRequest);

As the response will be received JSON Object about the channelID = UCrM1_toI5TsDYP1ZgeP91fw (athebula.com official channel)

The String url is the endpoint and we have to add the API key that we created on step 01

Although, not required to set OAuth consent in this project, as a best practice developers should update it.

And don’t forget to add restrictions on API key you created.

If you have any issue related this topic please comment or send us message through the social media

The source code can be found on github repository

Happy coding and chill

0 0
Happy
Happy
100 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleppy
Sleppy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *