Build a React.js Youtube Data API to Fetch User Channel Videos With Google OAuth2 Login in Browser
Jan 9, 2025
Get the full source code of application here:
https://gist.github.com/gauti123456/ce6a5f5d66aed2aed561142007f43983
Show More Show Less View Video Transcript
0:00
uh Hello friends welcome to this video so in this video we will look at how to integrate the YouTube data API version 3
0:08
uh with authentication and display your Channel videos and we will also be able to
0:14
search the videos as well so it's a complete project inside react CH and we
0:20
have the actual demo that I'm showing you we have a login with Google button right here for the authentication and we
0:27
are running this application on Local Host this is my react Chas project and
0:33
if I click this login with Google button I will be authenticated and I will display the Channel videos and you will
0:39
see the latest videos will be shown right here this is my videos and we are
0:45
actually displaying the title of the video and a link the open Button we can click and I can open this video so you
0:54
can see the video is opening so in this way we can
1:01
basically open any video you can see and I can search for any video Let's suppose
1:08
I can want to search let's suppose PHP videos so I can just write here
1:14
PHP and you will see all the PHP related videos it is automatically filtering out
1:19
the videos which are actually having PHP so containing this in the title PHP PHP
1:27
you can see that let's if I want the videos only related to my SQL so as I
1:34
write my SQL in the search bar you will see only videos containing my SQL will come you will
1:40
see so in this way it's very easy to filter out videos inside your channel
1:45
this is you can open this video you will see that the video will
1:51
open so this is actually the application we will be building all this data we are pulling it after authentication from
1:58
your uh YouTube data API version 3 so after authentication you allow access to
2:03
actually fetch videos from your channel and display it in the browser and then we have the search filter here we can
2:09
actually search the videos so as I type it you will see it automatically the
2:15
videos will be filtered so as you submit your title all the videos will have that title you will see that so you will try
2:22
to build this application it's a very simple application at the same time you will be able to see how to integrate the
2:29
you data AP API version 3 inside react Chas and so for building this
2:34
application we are actually using some packages the first package we are using is this package which is Google API
2:41
script this package is responsible for actually integrating your Google API
2:47
inside your react Chas application and the command is very simple npmi G ai- script this is a module and
2:57
just go to your command line so just make a simple react CH project
3:03
and then just install this package also for building the user interface we are actually using react
3:11
bootstrap for building the interface so you need to install these three packages
3:17
first is the this script Google API script package and then for building the user interface react bootstrap and
3:24
bootstrap so these are the three packages which are needed I've already installed these three package so what I
3:30
will do and also for integrating the login user authentication for
3:37
specifically for react Chas application the login with Google button you do need to install this package as well which is
3:43
react Google login this will integrate the user
3:48
authentication login with Google so install this package as well so I've already installed all the packages so I
3:55
will just start the development server so these are the different components which are responsible this is
4:02
for the authentication search bar video item video list and this is a main component
4:07
app.jsx here we are importing all these components so it's a complete project if you need the full source code the link
4:13
is given in the description so now let me start it from scratch let me delete the components folder so we have let me
4:21
create a very basic functional component you'll see nothing is there so if you refresh it it will automatically Auto
4:28
reload so if you now refresh this
4:37
application so now you can see app is showing so H Auto reload is working so
4:43
now the very first thing we need to do we need to impact uh import all the packages which we installed so the first
4:49
package we need to install sorry import is the Google API so this will be imported from this package that we have
4:56
Google aps script and then we also need to import bootstrap we need to import the CSS file
5:03
which is bootstrap test CSS bootstrap main. CSS and now we need to create two
5:10
variables first is the API key and the second one is the client ID so both is
5:17
will be const variables so it will be constant in the application so now to fetch the API key and the client ID you
5:23
do need to create a Google Cloud console account inside so you need to have a Google
5:29
Cloud console account and then you need to enable the apis and services go to the section and enable that YouTube data
5:37
data API so just click this button search for the API that you are
5:43
working with so there are lots of apis here so you can search for your API YouTube data API so you need to enable
5:51
this API so just click on enable so after
5:56
clicking enable this will change to manage I want already enabled that now to get the credentials you need to go to
6:03
the credentials option right here and just click on create
6:09
credentials and for the client ID you need to click on this option o or client
6:14
ID and the application type will be web application just simply select that and
6:20
now in these two options which is authorized JavaScript Origins authorized redirect URI you need to copy paste your
6:26
home URL this is a URL you need to copy paste so what I will do I will simply copy this and uh paste it right
6:35
here in both these sections and make sure this trading slash is not there in
6:40
the first one and click on this create opt create button so now it will give
6:45
you your client ID and this will be different for you just copy your own
6:51
client ID and paste it here and then for the API key uh we just need to have in
6:58
this section click on API key section and simply create your own API key so
7:04
this will give you the API
7:10
key so again copy to clipboard and just paste it so just after getting all these two
7:17
information now we can create some State variables first will be for keeping track of the authenticated if you are
7:24
authenticated or not this will be a Boolean parameter the initial value we'll be using use State hook it will be
7:31
false so the user will not be authenticated whenever you load the application and then we need to keep
7:36
track of the videos which will be fetched so you'll be fetching these videos in an array so the initial value
7:43
will be empty array and the next will be for storing the access token as you all
7:49
know in order to communicate with the Google apis you need the access token so this access token will be available
7:56
after authentication so you'll be storing it in this variable so ini value will be empty and now we also need to
8:02
keep track of what search query the user will type we also need to store the
8:07
search query set search query initial value again will be nothing so now we
8:13
have these four state variables so now after this now we need
8:19
to come to the jsx part so right here we will have
8:26
the container class of bootstrap all these are bootstrap classes and we will
8:33
be having the a simple heading in the center position and you simply say
8:41
that your latest YouTube videos something like this and now we need to
8:49
conditionally show that login button if the user is not authenticated you'll simply check in this jsx expression that
8:56
if this variable that we have this Boolean parameter if this is false in that case we need to
9:03
show the authentication but if this varable is
9:09
uh true in that case we need to show the actual videos of the
9:18
channel so right here we
9:26
can you are authenticated we can simply say so here we need to first of all a
9:33
make a component which will be authentication component here we need to pass three things client ID on success
9:39
and on failure but if the user is authenticated we can say you are
9:44
authenticated for now but now we need to make this component so just create in the source
9:50
directory just create a folder called as components and we'll be making this o
9:57
component jsx simple functional component so we can now include this
10:04
simply by typing Au so automatically it will be imported sorry not this
10:10
component we need to import we delete we need our own custom
10:18
component that we created which is this one it will be coming from this
10:24
component so this component here in this component we will be passing three three things first is the client ID so we have
10:32
stored this in the variable client ID the second one will be a function which will be on success this will be a custom
10:40
function that we will make so whenever the user is authenticated this function will get executed and similarly we will
10:46
have another call back function if any sort of error take place in the failure section we will use basically make this
10:54
function so now we need to Define these functions at desktop so we can simply Define the both these
11:10
functions so now we can actually go to the actual component which is the
11:15
authentication component and simply make it so now we actually are receiving
11:21
these three parameters to this component first is a client ID then these are the two call back functions on success and
11:28
all on aure so now what we need to do right here inside this component we just need
11:35
to return jsx expression and this will be the simple component which will be Google login it will be coming from
11:42
react Google login it's a simple login button that will be shown to the user and here we need to pass some things to
11:49
this component which is the client ID so we are getting the client ID and then we need to have the button text this will
11:56
be simple login with Google the third one is the on success so we
12:02
have this function which is on success and the fourth when the on failure so
12:07
again we have this on failure and here we do need to provide the Scopes as well
12:12
Scopes are information what information you are requesting it from the Google API so we need to access the information
12:21
of au scope we need all the Scopes we can read
12:26
information from the YouTube channel so we only need the read read only
12:33
permission so we only need to get access to the YouTube channel information such as the videos so that's why we are only
12:39
requesting the read only scope that's all so now if you refresh your
12:46
application you will see a login button appearing so if I start the application so what you will see a login
12:53
with Google button will you will see right here this button so if you click this button you will see this uh you
13:01
will automatically be logged
13:18
in so if you just refresh let me just
13:34
so this on success will be called here whenever you are successfully logined so
13:39
here we can simply inside the handle
13:45
login let me just write this so this response will be coming whenever you are successfully logged in so we can what
13:52
whatever is the response coming we can console log the
13:57
response and whatever is the error coming in case of any sort of error we can console like that also so if I rerun
14:07
it once again click on the login with Google button so if I go to console I will see
14:14
this uh information coming right here which is actually all this information profile
14:21
information about the user which is access token is also coming you can see this is access token we can need to
14:27
store this access token somewhere in order to access the information so this is my you can see
14:34
that the email address coming this is Google ID image all these things are coming so now we just need to store this
14:42
access access token some somewhere else so write in this
14:52
function so now after console loging the response
14:57
uh we just need to set this variable is authenticated from false to true so now the user is
15:04
authenticated and now we also need to store this access token using this hook function which is response. access
15:12
token and after you do this we also need to make a function which will be
15:17
fetching the videos from their YouTube channel we'll simply say response. access token we will pass the access
15:23
token as the first argument and the second argument will be nothing so now
15:29
we need to make this function this function will be responsible for fetching the YouTube channel
15:35
videos so we just need to make this function which is fetch
15:44
videos so this will be a Asing function and we just need to pass the token here
15:51
you're receiving the token is the first argument and the search query the second argument so by default search query is
15:57
nothing and now we need to use this module which is Google API that we are actually
16:05
importing it from this it contains a method called as load so we need to load
16:11
the YouTube client here this call back function will execute it will be a Asing
16:17
function so right in this function we will can use the await keyword because it's a Asing function so now this
16:24
function has a init function right here and here we need to initialize the
16:29
client by passing the API key parameter as the first argument so
16:35
we have stored this API key variable you're passing it and then it takes the
16:43
discovery dogs this will be array and it
16:48
basically contains the actual URL where you will be requesting the data
16:59
so this uh we will pass this URL so we are requesting the data from this
17:04
URL https Google ap.com disovery verion one API YouTube version
17:11
3 so we using the YouTube data version API 3 the rest API so after you just do
17:18
this uh now the next step is that we need to
17:23
fetch the videos from the API so for doing this we
17:29
make a simple fetch call so you will simply say Google API
17:34
it contains do client. YouTube and then we will use the actual
17:41
channels. list method and it takes an object and it takes three par parameters
17:48
first is a part parameter and this part parameter value you need to pass
17:54
snippit and then you need to pass comma content details we also need
18:01
the information about the video as as well so content details put a comma the second one is the parameter mine is true
18:08
this means that you need to fetch the videos of your own channel not third party videos so we will request the
18:15
videos from our own channel the channel associated with this email address the third parameter is the
18:21
access token that you need to pass with every request you need to pass this access token this is actually been three
18:28
parameters passed now we can actually request the channel
18:33
ID and this basically comes as response. result and it is stored inside item zero
18:42
and it has the ID of the channel so in this way you can request the channel ID
18:47
so once we get the channel ID using that channel ID we can request the videos so
18:53
we can just do this in try catch block so if any sort of error take place we can console lock that error so now in
19:00
this we can request the video list and for this we can again use this
19:06
STK it contains a method which is search method so in this
19:11
SDK the search method is available so this search method actually has a method
19:18
of list and this list method takes an object so this object will have some
19:23
properties again the part parameter is snippet the second parameter you need to give
19:30
the channel ID so we are actually getting the channel ID in the previous step so we are simply passing it the
19:37
third parameter is the maximum results that you want to fetch so only we only need to fetch the latest 20 videos from
19:44
your channel which we'll pass 20 here and then the C parameter so this simply
19:49
means whatever search query you are passing we are simply passing it and then lastly we need to also pass
19:57
the access token so X underscore token so we need to pass the token so we
20:03
are passing these five things part Channel ID maximum results Q is a search
20:08
query and access token so once you do this we will get the response and we can
20:14
use the hook function to set the videos and we can say video list response.
20:19
result. items this will be a simple array we have setting is using the hook function that we declared you will see
20:27
which is initially it is but now we are setting this the array which is coming
20:33
so once you do this uh now in the
20:40
jsx so I can simply console log the response which is coming I can write the
20:45
console log right just to show you in the browser let console log all this
20:50
information the channel ID as well and then this response which is
20:56
coming video list response so now if you go to the browser and if you just uh
21:02
click the
21:22
button uh let me just okay some error has there
21:39
let me rerun the application here npm run Sometimes some errors you can see
21:45
now it is running HTTP uh let me just see what is fail to
21:51
resolve entry for package htdp proxy agent
21:59
uh I think guys some type of mistake has happened that's why it is giving me this
22:05
error I think in this section we have made some error so let me just fix it by
22:11
pasting the code you can find all the source code in the description I think in somewhere I made
22:18
a mistake let me paste this code once again contrl C
22:34
so now you can see now the application is running so it was just a mistake in this you can just pause the video and
22:39
just see what mistake I done so this is actually the correct code so if I
22:45
refresh
22:52
now uh again the same mistake let me just see guys uh guys I fix figured out the error
22:59
uh basically accidentally I imported this line at the very top I'm not using
23:05
this you will see import Discovery from Google apis I just accidentally imported this line so just need to delete this so
23:12
once you delete this the code will work and refresh it you will see the login with Google button
23:18
so now if I click the button you will see now it is showing this message that
23:25
you are authenticated and if I inspect element and uh see the
23:32
result you will see this will be showing my profile
23:41
information so we are actually right here we have calling this
23:48
function so what I need to do I just need to console log this
23:55
response which is coming from the API and then from that we are extracting the
24:00
channel ID and from that channel ID then we are getting the L list of videos so
24:07
let me console log all these responses which are coming from the API so once I click login with
24:14
Google so now you can see first of all we get the profile information we get this response and then from this profile
24:22
information we get the access token and that from the access token we get this channel ID and you can see
24:29
we get the channel ID this is the channel ID that you can see so we get the channel ID and from
24:35
that channel ID we get you will see the channel name description everything and
24:41
then from that channel idid we get the total number of videos that we got which
24:46
is the top 20 latest videos you will see and for each video we have this
24:52
information which is the ID channel ID this is the snippet and if you see
24:58
snippit contains the channel information the publish date publish time thumbnails
25:04
so each sort of information is there and then we have the information related to
25:11
video as well this is a thumbnail this is the title everything so now we need to show this information in the browser
25:18
so it's very easy so now coming back to the jsx part so when you are authenticated
25:25
instead of this heading we just need to show the video videos so we will have a diff section and here we need to create
25:32
a component I will call that component at at search
25:37
bar so now I just need to make this components in the components folder
25:43
search bar so I just need to import this
25:49
component which simply I will say search bar
25:59
so what I will do I will import this
26:16
component you'll see that let we changed the
26:38
so now you can see it has been imported so now in this component we
26:44
need to pass three things first is the actual search query that the user will write the search
26:51
query variable so whatever is the search query we have storing it in the variable
26:56
and then the second one is the act ual function which will execute once the user writes in anything in the search
27:02
query we will execute this function handle search change you need to Define this function in
27:08
this and then the third one will be a on search so when the user starts
27:17
to search so you'll basically call this fetch videos function and again pass the
27:23
access token plus the search query that the user has entered so this on search
27:28
will be a simple call back function so here we do need to Define on
27:34
search change we need to Define this
27:42
function handle sech change so now in the search bar
27:49
component if you see we need to now we are receiving three these three
27:56
parameters first is the actual search cury second is the on search change call
28:04
back function the third one is on search so now in this component we need
28:11
to use some bootstrap for showing uh the search field we'll be using the
28:19
form component you will see it will be imported from react bootstrap we need to use the bootstrap
28:25
class margin bottom three
28:32
so in this we need to have this will be of type text so this will be a form element
28:40
so let me inside this we need to define the
28:47
form
28:55
control so this will be of type text and here we need to give the
29:01
placeholder which will be search videos and the value here uh which will
29:08
be we need to bind the search query which is coming right here in this component and then the on on change
29:15
function so whenever the value is change of this input field we need to bind this function which is we are defining which
29:21
is on so change and also we need to give it a class name of
29:29
me to so if you now refresh now you will
29:35
after you click the button you will see the search input field the search bar you will see
29:41
that you will not see the bootstrap because we do need to import the bootstrap file here
30:09
this is a search
30:17
field form control um
30:30
let just wait let me just paste
30:45
this okay I think we are not calling this uh in this appjs
30:51
function we having this div section right here
31:03
so we have this app section right
31:17
here search for click on
31:23
that so we have this search bar field and now after this we need to create
31:28
another component which will be responsible for showing the total number of videos from the channel so this will
31:35
be video list component this will be a simple
31:42
functional component and after this search field we will show
31:47
this component which is video list and this component will take uh the videos
31:52
as the first parameter so videos we have the array we are storing it and then this call back function which is on open
32:01
so this will be a simple call back function which will take the video
32:06
ID this will be a function which will be responsible for opening the video in a new tab so we are passing the video ID
32:12
as the argument so I will just console log for
32:19
now so first of all we need to receive this information in this component which
32:26
is video list so in this video list component if you
32:31
see we are passing the first parameter is the videos and then the call back function on open so right here in this
32:39
we need to actually create a unordered list UL
32:46
element and we need to give it a class name of list
32:51
group these are all bootstrap classes
33:00
and here we need to Loop through all the videos using the map operator and for each video we will
33:08
be having another component here which will be video item and we need to pass the key here
33:15
which will be coming from video ID so we are simply passing the video ID
33:21
as the key and the actual video we will pass as this object and then we will
33:28
pass the third argument is on open so this is a function that we are passing so we need to create another component
33:34
where we'll be showing the actual video so again in the components folder just create video
33:43
item so this will be a function component and we can just import this
33:48
component here which is we can simply say video
33:54
item this will be imported if you see so now in this
34:02
component we are actually passing uh two things what is the actual video
34:09
and then the function which is on open so now from this
34:15
uh we will actually have a Alli
34:22
tag and we will be giving the bootstrap class list group item
34:34
deflex justify Center justify content
34:41
between so these are all bootstrap classes and now to align the videos in
34:47
the center position align items
34:52
Center so right here we will be embedding the title of the video so will simply show the title inside C brackets
35:01
and then we'll be having a bootstrap button and the variant will be primary
35:08
button and the size here will be small basically binding on click
35:14
function when we click the button we will be binding this on open function and we'll pass the video ID as the
35:23
argument and we'll be giving a class name of this bootstrap
35:33
class so if you refresh
35:40
here so it is saying that title is not
35:47
defined okay we do need to import the information about the video from this video object so we need to extract the
35:55
title here from video do snippet do title so if the title is not
36:01
accessible then we can simply say no title same goes with the video ID as
36:07
well you can extract the video ID from this object which is video. id. video
36:17
ID so if the video ID is not available in that case we can simply return n
36:32
so you will see all these video titles will be shown right here top 20 videos you will see
36:38
that and then this open Button you need to show the label of the button as well
36:43
so what I need to make sure it's say
36:54
open so my top 20 videos are showing right here here and we click the open
36:59
button I need to bind a listener here we are executing this on open function
37:05
because we are passing it again right here we are getting this from the video list component and here we are passing
37:12
this on open function and instead of console loging the video ID we need to
37:19
actually open the video in a new tab so for now opening that
37:25
video we can simply say the video it ID has been there in that case we need to
37:31
open the video window. open so you use the backtick symbol and
37:38
uh we'll open this youtube.com/watch
37:43
and we just need to pass the video ID question mark V is equal to and then we
37:49
need to pass the video ID which is like this comma and then we need to open it
37:57
in the blank window we can simply say add underscore
38:04
blank so if you refresh
38:11
now so if you click any of the video open Button you will see the video will
38:17
open in a new tab so in this way uh the videos are
38:24
displaying right here and now we need to implement the searching functionality we need to
38:30
display a search field where we can search the video so now in the search bar component if you
38:46
see uh you can see we I think we defined this function which is handle search
38:53
change yeah I think we defined that so this event will be there whenever we
38:58
change the value it will automatically be passed
39:05
and we just need to say set search query to be whatever is event. target. Val so
39:12
whatever you
39:22
write so we have written this
39:29
so this is actually your search for component
39:55
and uh let me see what is the problem here why the search field is not displaying because if you see only the
40:02
search bar is
40:08
displaying oh I think that's the problem if you see we actually misspelled this
40:14
this needs to be Capital because if you see the file name right here it's s is capital here just make sure s is capital
40:23
here
40:36
let me include that once again search
40:47
bar so now it is included
41:02
uh I think guys we misspell this so let me change the component name uh let me
41:09
say here search
41:25
field so let me just import this delete
41:41
this so now this will be imported uh you see search field component and hopefully
41:48
now it will show it will now see it was just a smelling mistake now we have the
41:54
search field if I write something here nothing happens if you see we are not
42:00
doing uh the filtering process now we doing that I have already attached a
42:09
method uh to this search whenever you we are cing this search field component if
42:16
you see we have this on search function executed which is fetch videos
42:28
on search function is
42:33
there if I console lock we call
42:45
L so now if you click on the search button you will see all the laral related videos will
42:52
show after writing laral you do need to click the search button so after that
42:57
you will see all the laral videos will show you will see the title will contain
43:02
larable same goes if I search for MySQL click on search you will now see all the
43:09
videos related to mySQL will show so in this way you can filter all your videos
43:15
all your Channel videos based upon a title using this simple project and you
43:21
can do a lot more with this project you can extend this project you can add a filter for date as as well you can
43:28
receive input if you want a videos related to a specific date you can also
43:34
do that as well the code will remain the same the base code so all the source
43:40
code is given in the description guys this is a full project that we made it compos of four components authentication
43:47
component search field video item video list so we have used the YouTube data
43:54
API version 3 for getting access to all this information coming from your YouTube
44:00
channel we're displaying the top 20 videos you can also display top 50
44:05
videos as well you just need to change the value whatever from 20 to 50 right
44:11
here so that's all guys this is thank you very much for watching this
44:16
video please hit that like button subscribe the channel and do check out my website as well free mediat tools.com
44:21
which contains thousands of videos related to audio video and image and I
44:27
will be seeing you in the next video
#Computer Education
