YouTube Developers Live: From API reference to code in minutes

YouTube Developers Live: From API reference to code in minutes


MALE SPEAKER: –paying
attention. MALE SPEAKER: Sounds good. MALE SPEAKER: [INAUDIBLE]. MALE SPEAKER: [SINGING] MALE SPEAKER: Come on. [INTERPOSING VOICES] [MUSIC PLAYING] IKAI LAN: Hey, everybody. Welcome to another edition of
“YouTube Developers Live.” My name is Ikai Lan. JEFF POSNICK: And I’m
Jeff Posnick. IKAI LAN: And we’ve got a great
show for you guys today. So before we get started, it’s
probably a good time for us to plug Google I/O. Now,
tell us a little about Google I/O, Jeff. JEFF POSNICK: Yeah, I will plug
the recaps of Google I/O. IKAI LAN: [LAUGHS] JEFF POSNICK: Slightly late to
plug the current year, but we do, fortunately, have a great
set of session videos for all of the YouTube sessions and all
of the other Google API sessions, and they’re
available on YouTube right now. You could watch them all. They’re about 30 to 40
minutes for each one. And the best way to get all the
list of YouTube-related sessions is to visit our
dedicated YouTube channel. So that’s
youtube.com/youtubedev, and we have a playlist going there to
look at all the other videos mixed together. It’s on the channel that you’re
watching right now, which is
youtube.com/googledevelopers, so there’s a ton of
great stuff there. IKAI LAN: I was just hoping
you would say, so tickets are sold out. Also, the event is over. Anyway, enough of that. So today, we’re going to be
talking about a couple of things that are very important
to developers. Now, Ibrahim, can you go
to the slide screen? So Ibrahim’s helping us
produce in the back. Sometimes, you’ve seen him
on different shows. Today, we’re going to be talking
about how to use the API Reference and how
the API Reference translates into code. Now, we’re going to show you how
to use the API Explorer, how these actually map to HP
requests, then we’re going to go ahead and do some live
coding in Java. I’ve also got some Python
samples, but I’m not going to spend a lot of time in it. Now, what’s really great is you
might be asking, Ikai and Jeff, I don’t use
Python or Java. I use– JEFF POSNICK: Ruby. PHP. IKAI LAN: I use Ruby or PHP. So the good news is that all of
these will translate very well to the other languages,
and you just have to take a look at the code samples
we posted on developers.google.com/youtube
to see where the differences are and how to do
a translation. But everything that we talk
about today is going to map very well to the other
languages. JEFF POSNICK: Yeah. It also maps well
to other APIs. So if you’re a YouTube Data API
developer, you’re getting used to our client libraries,
but if you ever start using Google+ APIs, the Drive APIs,
some of those other Google products, we have the same
client libraries that pretty much operate in the same
way regardless of which API is there. IKAI LAN: That’s right. JEFF POSNICK: So it makes it
easy to mash up different APIs in the same application, and
everything that Ikai is talking about today will apply
to developers using other APIs as well. IKAI LAN: Now, that’s
enough hand waving for you guys today. We’re going to show you what
we’re talking about. So I’m going to go ahead and
open up Google Chrome. Now, I’m going to mention
everything we’re talking about is about the V3 API, so
you should be using this, the V3 Data API. So I’m here right now in our
favorite site, and I’m going to try to blow this
up a little bit. This is
developers.google.comyoutube. And if you can’t find this page,
you can just go to your favorite search engine and
search for “YouTube” and “V3 API,” I guess. JEFF POSNICK: Sure. YouTube API. IKAI LAN: I don’t know. I always have this tab open. JEFF POSNICK: I google myself. I search for “Jeff Posnick” and
I find all the articles I write and stuff. IKAI LAN: Now, I’m going to come
over here, I’m going to click on the right-hand
tab, and I’m going to go to Version 3.0. And you’ll see that all of
our stuff is here in the documentation, and we’ve also
got an API reference. So the first thing that we’re
going to do is we’re just going to look over this API
reference right here. Now, I’m going to spend the next
half hour going through every method and every call. Just kidding. I’m not actually going
to do that. Instead, we’re going to a quick
introduction into what this actually means. So let’s take a look and let’s
go to the API Explorer and give you a preview of what
we’re talking about. So I’m going to go to Videos,
and I’m going to go to List. Now, don’t worry if this doesn’t
make any sense yet. I will explain what
this means. So this is what our API
reference looks like. We have the methods. We have the parts. We have what the response
might look like. And at the bottom, what we have
here is we have the API Explorer, and this allows us
to test our API without writing any code. So let’s go ahead and just make
a quick API call now. Let’s see, id,snippet. Now, I’m going to go ahead
and pull a video. This is one of my first shows
I did for YouTube. I’m going to plug this in
here into id, and I’m going to hit Execute. Now, you’ll see that what this
is doing is this is making an API call and it’s returning
some data about my video. My video here is Things New
Developers Say, and here’s a description. So now if we come back to the
API Explorer, you’ll see that this maps right here in a much
more machine parsable format. Now, let’s review what this
is actually doing. So what this API Explorer is
doing is it’s helping us make HTTP requests to the API server
and it’s printing out the data in JSON. So what does this HTTP
request look like? Well, we’re doing the Get. We’re calling googleapis.com/y
outube/v3/videos, and we’re passing it some data. And this is because the V3
APIs use what’s called a RESTful style. And in a RESTful style, we
have a verb and usually a resource because a lot of API
calls tend to follow this convention of the verb usually
maps to what do you want to do, and the resource is, well,
what do you want to get? What do you want
to operate on? So what that looks like is in
the RESTful style, a verb typically maps to a particular
operation. And when you take a look at the
API Reference for YouTube, you’ll see that the verbs map to
certain API calls, and I’ll show you what this means,
whereas a resource can be a video, a channel, a playlist,
or anything else. Now, if you’re working with
the Google Drive API, this might be a folder. It might be a file. I don’t know what the exact
things are called. I’ll know if I look in
the API reference. JEFF POSNICK: It’s also worth
pointing out that that particular API call can map
to multiple resources. You could put in a list of
different IDs there and get back information about
all of them. IKAI LAN: That’s correct. JEFF POSNICK: In this particular
example, you’re just doing one, though. IKAI LAN: I’m just
doing one, yes. So when we take a look at these
methods here, you’ll see that there’s a list, insert,
update, and delete method. So whenever you’re in the API
Reference, you can click on the resource that you want to
get at, and then you’ll see the different methods, which
are what you can do. Now, list will usually be a Get
operation, and it’ll map to give me some information,
whereas insert will be a new object being added. Update is usually updating an
existing object, and delete usually deletes the object. Now, video also has some– I don’t want to say they’re
nonstandard, but outside of the four verbs, we can also get
a rating, and we can also rate videos. So another way that this could
have been structured, for instance, I’ve seen this done
on different sites, is we would have a ratings resource
and we would insert or update. But really, this is just
a matter of style. And this is perfectly correct
and it works too. So let’s go back to the API
call that we just made. Now, what did we really just
do underneath the hood? Well, we did this. Entered in the part, we entered
in the ID, and we executed this, and the
API Explorer made an HTTP call for us. So what we’re going to do now
is we’re going to show you using cURL, which is a command
line tool available on Unix-based systems, be
it Linux, OSX, BSD. Did I list them all? JEFF POSNICK: I’m sure people
have ported it pretty much everywhere. IKAI LAN: You guys know what
we’re talking about. Haven’t seen this on Windows,
but I’m sure there’s a Windows version somewhere. JEFF POSNICK: There’s got to
be HTTP user agents for the Windows command line. IKAI LAN: So cURL allows us to
fetch HTTP, and this is what the browser does, except the
browser also takes the data and it renders it for you. So cURL is just going to give
us the raw output, whether it’s in XML, HTML,
or whatever. So what I’m going to do now is
I’m just going to show how the API Explorer translates
to cURL. So Ibrahim, can you go ahead
and put some– thank you. He’s such a great producer. So I’m going to go ahead now and
show you what this looks like if I do curl http. Let’s mix it up a little. My slide says Google. Let’s see what YouTube’s
home page looks like. Whoa. There’s a lot of stuff
going on here. And you’ll see that this is just
HTML, and that’s what we expect to see. Now, when we go to the browser
and we go to youtube.com and we View Source, if I can
find where to do it. JEFF POSNICK: Just right-click. Yeah. IKAI LAN: View Page Source. You’ll see that it’s going to
look very similar, if not the same thing. So that’s what’s cURL does. And now let’s go and see how
we can use cURL to do some things with the API. So you’ll see that right here,
we’re making a Get request and we’re fetching this
string right here. Now, just a caveat, at the end
you’ll see your API key. I’m not replacing that only
because I’m curious to see what will happen. I think this still might work. JEFF POSNICK: I think you’re
overly optimistic about that. IKAI LAN: Oh, I’m wrong. It doesn’t work. And if you see, I’m getting back
JSON, and this is what the server’s telling me. It’s telling me that the
key is invalid and this is a bad request. So I’m going to have to go ahead
and fix that, and to fix that, I’m going to go
to the API Console. And to get to the API Console,
you’ll go to code.google.com/api/console,
and here I am. And don’t bother stealing
these keys. I’m going to change them after
the show and break all of my test code, by the way. JEFF POSNICK: Nice. IKAI LAN: So I’m going to go
ahead and copy the key, and the key that I’m going to
use for this API call is a simple API key. Now, the difference between a
simple API key and a client ID, client secret key is that
the simple API key can be used most places where you don’t need
an authenticated user. So the way I like to think about
it is what can you do on youtube.com without
logging in? And those are things that you
can do with a simple API key. And one of the things you can
do is get information on a video because you need
to watch a video. JEFF POSNICK: And this is
actually a place where the Version Three of the API
differs from the older versions, Version Two, Version
One, in that we did allow really completely anonymous
access in Version Two and Version One. So you might be familiar with
just being able to make these requests without having
to register for a key. You do have to register
for a key in Version Three, but it’s easy. Just one piece of information
to keep track of, basically. IKAI LAN: Thank you for buying
me some time, Jeff. So I’ve gone in and I’ve copied
the key over to the command line. It looks like I’ve got this
put out correctly. So now, let’s go ahead
and run this again. You’ll see that I am getting
back the data about this video, and it’s the same thing
that’s happening over here in the API Explorer. So you don’t have to
do it in cURL. Again, cURL is just another
way you can do this. And one of the reasons cURL is
nice is because when you’re actually writing code, it may
be easier for you to output. Say hey, what’s the URL I’m
fetching and what are the parameters I’m passing? And pass them to cURL instead
of going back to the API Explorer and plugging
the values back in. Now, the API Explorer still
has a lot of neat functionality, and we’ll get
into what some of that functionality is because when
you start to make more and more complex requests, it
becomes a lot easier to build out the API call in the API
Explorer first before translating it into code. JEFF POSNICK: And there’s nice
documentation for each of the parameters and the API Explorer
also, which I’m sure folks who go there
will appreciate. I know I always appreciate it. IKAI LAN: He appreciates it. JEFF POSNICK: Yes. Good job to our tech
writing team. IKAI LAN: He also wrote some
of the samples and the docs and stuff. So let’s go ahead and let’s
put this into practice. You want to put this into
practice, Jeff? JEFF POSNICK: I would love for
you to put it into practice. I would love to observe. IKAI LAN: I will put it into
practice, and he’s going to observe me. That’s what we’re
going to do now. We’re going to do something
that a lot of people have asked for, which is we’re going
to write some code that lets me fetch my likes, the
videos that I’ve liked. Now, a lot of people have
come and asked us, well, how do I do this? How do I write some code
that fetches my likes? And as of V3 of the API, there’s
a very interesting approach to how we do this, and
I think it makes sense. So what we do when you want to
fetch the videos you’ve liked, is that your likes belong in a
playlist, and that playlist is part of your channel. Now again, you might have
multiple channels, but each channel will have one
Likes playlist. So what we’re going
to do is we’re going to get that playlist. We’re going to get
the Playlist ID. Then we’re going to make an API
call against the Playlist API to find all the videos that
belong on that playlist. Does that sound good to you? Am I leaving anything out? JEFF POSNICK: It does
sound good to me. This is the same sort of method
you could use to get a list of all the videos that are
in any playlist, or your Uploads playlist. If you wanted to get all the
videos that are uploaded in a specific channel, same
method, just slightly different playlist. IKAI LAN: Watch Later
playlist. But these are all the concepts
of a playlist. Again, you can use this
for Get My Uploads. The only reason I’m changing
this around a little bit is because we already have a couple
of code samples for Get My Uploads in pretty much
every language. So I’m only modifying this a
little bit, but really, I think the code is very much the
same except for one string substitution. Oops, getting ahead of myself. So now let’s go ahead
and do that now. I am in the API Explorer, and
like I mentioned, I need to fetch your channels. I need to fetch your channel and
I need to figure out, hey, what is the ID of my
Likes playlist? Now unfortunately, it’s not
a predictable string. You can’t just take your
channel name, append -playlist, and have
that be your ID. So instead, we’re going to have
to go ahead and query the API and figure out what the
ID of that playlist is. That make sense? JEFF POSNICK: It does. You talk about you here,
knowing your playlist. So I assume there’s going
to be some sort of– IKAI LAN: It’s going to be my
playlist because we’re going to be from my test account. JEFF POSNICK: I know, and it
seems like there has to be some component here where the
code actually knows who you are, so hopefully you’ll
talk about that, too. IKAI LAN: He’s trying to lure
me into talking about authentication. JEFF POSNICK: I bait Ikai all
the time to try to get him into talking about OAuth2
because there’s nothing Ikai likes more. IKAI LAN: He actually
does do this. We have an article
coming out soon. No ETA on that. So let’s go ahead and start
playing around with the Explorer right now. Now let’s go find my channel. I’m going to click on Channels
over here in the API Explorer. I’m going to zoom in on
this so you guys can see what I’m doing. Now, a Channel Resource contains
information about a YouTube channel. All I did was read that
off the documentation. So we can update data about
the channel or we can find information about a channel. If we want to find information,
it’s a List operation, so I’m going to
go ahead and click List. Now, I’m going to go ahead and
scroll down, and you’ll see that I have the API
Explorer here. I’m not going to enter anything
in here, and I’m just going to run this thing
and see what happens. It’s not going to work. I’ve got an error. What’s the error? It’s a bad request. Well, I need to put in a
required parameter, which is part, so let me go ahead
and do that. Let’s get
id,snippet,contentDetails. And where did I get
these values from? Right here in this paragraph. Usually, most of the things
that you want tend to come from id,snippet or
contentDetails. They vary slightly in what they
do, and we’ll experiment a little bit with this
to see what it does. I’m going to go ahead and
hit Execute again. I have another error. I didn’t pass in a filter. JEFF POSNICK: There you go. IKAI LAN: So when you’re
searching for channels, there are a few things that
can happen. You might have to enter a filter
to say, I want you to narrow down the types of
channels I’m looking for. Now really, I’m just looking
for my channels. So right now, I’m logged into
my test account, and my test account is
[email protected] It’s named after a stuffed
dinosaur I keep on my desk at work. So I’m going to go ahead and
scroll down first and try to add a filter, and the filter
here is going to be mine. Do I just want to get
back my channels, Dino Dinosaur’s channels? I’m going to click true, and I’m
going to try and execute this again. More errors! What’s going on? It’s a 401 unauthorized. And we’re saying, no, this
is an authorized request, authorization required. What do I do? Also, there’s a big red bar here
that says, this method requires you to be
authenticated. You may need to activate the
toggle button above to authorize your request
using OAuth 2.0. JEFF POSNICK: Very
handy hints. IKAI LAN: I forgot to hit
this button, Jeff. It was intentional. I’ve been in control of this
show the whole time. JEFF POSNICK: It’s not the dino
in the background pulling your strings? IKAI LAN: All right. So now, once I click this, it’s
going to ask me for which OAuth 2.0 scripts I want. I’m just going to
click Authorize. But again, this is
a good place to test different scopes. I don’t need the
partner scope. I don’t need the upload scope. Really, I only need the read
only scope, so I’m going to release this right here. I’m going to click Authorize. So what should happen is you
should get a pop-up box that says, hey, do you want to grant
the API Explorer access to your account? And you click OK or Cancel. I’ve already done that
in the past. That’s why it’s not
showing up. JEFF POSNICK: And that’s
actually a good thing. It’s less things for
your users to have to click through. If they’ve already granted
approval once, we know about that. We don’t have to
ask them again. IKAI LAN: But it’s bad for the
show because I can’t show you what it looks like. But you’ll see what this
looks like when I run my sample, I hope. JEFF POSNICK: Maybe you could
do super slow motion of that little pop-up or something. IKAI LAN: Whoosh. We’ll have to come back
to edit this later. Anyway, let me go ahead
now and scroll down and hit Execute. Oh well, what do you know? There’s my info right there. JEFF POSNICK: Very cool. IKAI LAN: So it has
information. My name is Dino Dinosaur, it’s
the name of my channel. And this is just
a stock photo. It doesn’t mean anything. And here are my different
playlist IDs. So again, let’s go ahead
and run this in cURL and see what happens. So we’re cURLing this right
here, and we’re calling Get on this URL, but we’re going
to have to pass in some special headers. Now, it turns out I did
put it in here. So we can add headers using the
-H and a “Header:value”. So I’m going to go ahead and
construct this cURL command before I paste it to
the command line. So I’m going to do this now. There we go. Hopefully, you guys
can see this. What do you think? Bigger? “curl.” We can do bigger
than that. We can do it a little
bigger than this. JEFF POSNICK: Make it bigger. There you go. IKAI LAN: Make it bigger. JEFF POSNICK: I’m not
wearing my glasses, so I can’t see anything. IKAI LAN: This is only
going up by one. How’s this? “curl.” Now, let’s go ahead
and cURL this URL, so I’m going to go ahead
and copy, paste. Of course, it chose to copy
formatting information. JEFF POSNICK: It’s a
well-formatted URL. IKAI LAN: It is a well-formatted
URL. JEFF POSNICK: What more
can you ask for? IKAI LAN: So you’ll see here
that this says, Your_API_Key. We’ll want to replace
this with our API key, as it suggests. Let’s go back to the
API Console. Now, remember earlier when I
said you use a simple API key for simple requests? JEFF POSNICK: Yes. IKAI LAN: That would be for
requests when you’re saying that’s public. But now, I’m acting on
behalf of a user. Really, I’m just acting
on behalf of myself. I don’t want to get too much
into what this means, but anytime a user needs to be sent
through the OAuth flow or prompted for authentication,
this is when we would use the client secret. So let’s go ahead
and copy this. So we’re going to use the client
secret and not the simple API key. JEFF POSNICK: And you’re
putting that– hmm. IKAI LAN: I am going to put– JEFF POSNICK: You
know what, Ikai? You don’t actually
have to do that. IKAI LAN: Do you not? JEFF POSNICK: No. This is not part of the show. We’re going off script. IKAI LAN: I tested
this last night. JEFF POSNICK: It won’t hurt,
but I will use this as a teaching opportunity to point
out that anytime you’re actually including OAuth2
credentials in the request– IKAI LAN: Perhaps we
should take a bet. JEFF POSNICK: OK. IKAI LAN: What would you
like to wager me, Jeff? JEFF POSNICK: I don’t know. IKAI LAN: How about your
first born child? JEFF POSNICK: Hey. That’s my son. IKAI LAN: He actually has a
child, so it’s not a real Rumpelstiltskin thing
going on here. JEFF POSNICK: No. Whenever you actually do
include OAuth2 actual credentials, which I assume
you’re planning on doing– IKAI LAN: I am doing
that right now. JEFF POSNICK: It’s
not necessary to put in an API key. And if you do put in an API
key, it will basically continue to work. IKAI LAN: Well, let’s
test this right now. So I’ve gone ahead and
I’ve added this into my cURL command. And you’ll see I’ve added -H.
Now, this is all one line. I’m not adding in line
breaks in here. We’re going to add in
this authorization and a bearer token. This in OAuth is what we
call an access token. JEFF POSNICK: Yes. IKAI LAN: Yes. JEFF POSNICK: This
is [INAUDIBLE]. IKAI LAN: I get the
names mixed up. There’s an access token. I guess this is actually a
bearer token, but I guess that that’s [INAUDIBLE]. JEFF POSNICK: It’s a type
of access token, yeah. IKAI LAN: Yeah and sometimes you
use a refresh token to get a bearer token. It’s a little bit complex and
we’re not going to get too much into that today, but
we do understand it all. JEFF POSNICK: We’re not making
this up as we go along. IKAI LAN: I’m going to go ahead
and copy and paste this into here, and it looks like
I’ve lost the bet. So I will tell you this. I think the reason I think
that this didn’t work is because I actually copied in the
simple API key, and that causes something weird
to happen. But we’ll find out right
now since we can. So I’m going to go ahead and
copy the simple API key in. Let’s see right here. And key equals blah. And again, this is annoying
because it’s copying the formatting. Let’s copy, paste in here. And there, you will get
invalid credentials. So if you are getting invalid
credentials if you’re hand rolling your own code, make sure
that you’re not passing a simple API key. Make sure that you are taking it
out, or [INAUDIBLE] secret. JEFF POSNICK: Or putting
some random value in. Apparently that’s better
than actually– IKAI LAN: Well, I don’t know. I don’t think the random
value will work. Let’s try that. Let’s give that a try. Random value. I am really– JEFF POSNICK: That’s
not random, though. IKAI LAN: There you go. You’ll still get invalid
credentials. So let’s do one more test,
just for the LOLs. Just for the LOLs. JEFF POSNICK: That what
the show’s all about. That’s what it’s devolved
into, nothing else. IKAI LAN: That’s why
I do this show. We’re going to go ahead and
copy and paste this, and look, it works. So I feel like I win
part of this bet. JEFF POSNICK: Part of my son–
we’re getting into the King Solomon situation, chopping
up my son. IKAI LAN: The King Solomon
situation. JEFF POSNICK: I don’t know. IKAI LAN: We’ll have to
edit that joke out. JEFF POSNICK: OK. IKAI LAN: I’m just kidding. We’re not going to
edit this show. [LAUGHTER] IKAI LAN: So let’s go back
and see what we did here. Now again, I feel like I’m
proving to you guys that this works, and that this really is
just making the HTTP request. The API Explorer is just really
nice in helping us format some of these parameters,
and I’ll show you what this means. Now really, the only thing that
we need from here is this right here, is this Likes ID
from the Likes playlist. So I’m looking at all this
other data, and I don’t really need it. So this Likes ID comes under
Content Details section. So why don’t I go ahead
and remove ID and snippet from here? Does that sound OK to you? JEFF POSNICK: Sounds good to me,
and that’s what we would recommend doing, too. IKAI LAN: We recommend
just asking for exactly what you want. This is especially important if
you’re working on a mobile platform because you’ll
make fewer requests. And everything’s going to be
gzipped anyway, so it’s all going to be small and it’s
not going to take up a lot more space. In fact, it’s going to take very
little, but it’s just the best practice. It’s just keeping your
house clean. JEFF POSNICK: And there’s quota
benefits too, and all sorts of good reasons. IKAI LAN: So let’s take a look
now at the response. Well, still got a lot of info. We still don’t really
need this here. And I don’t really need the
googlePlusUserID, so why don’t we go ahead and filter this
down a little bit? JEFF POSNICK: Cool. IKAI LAN: So that’s
done in the fields parameter of the request. So I’m going to click here
and, well, I can type anything in here. JEFF POSNICK: I wonder– IKAI LAN: Let’s see
what happens. It blows up. It’s a bad request. This thing is strict. So I’m going to click
instead on this tool called the fields editor. Now, I can select everything
but I might as well not at this point. Let’s just execute
this correctly. You’ll see that this is
in the items tag. So when we go back to the fields
editor, let’s expand the items tag. We really only care about
the contentDetails. There you go. I think this is going
to give us the exact same thing, actually. JEFF POSNICK: No, it’ll
be a little less. IKAI LAN: Yeah, you’re right. So you’ll see that, again, this
is all we’re getting. We’re getting back items
and contentDetails. If I wanted more than this, I
would go back and add more fields, like what do I want? How about the nextPageToken? I’m not going to talk about
it this show, but the way pagination works is you pass
the nextPageToken into the next API request. So when I scroll down here and
there is no next page, therefore, there’s
no nextPageToken. But what you would do is when
you do get back a token, you would pass it into this
pageToken parameter, and that’s how you go on paginating
your request. JEFF POSNICK: And another way
you might use this, if you know that you only want the
Likes Playlist ID, for instance, you can drill down
even further within contentDetails and say, only
return the Likes Playlist ID. But you don’t need to. IKAI LAN: So I’m going to
go ahead and copy this right now, this ID. And let’s go to a
different page. Now again, let’s go to the API
Reference and let’s click on Playlistitems. We’re going to click on list,
and it looks similar. So let’s scroll down. JEFF POSNICK: Passing
all the sample code. IKAI LAN: Passing all
the sample code. We’re going to authorize
the request. Click Authorize. Let’s see. Let’s just get everything for
now to see what we’re getting. Now let’s scroll down and
put in a playlistID, and let’s hit Execute. And what do we see now? Well, here we go. We see all the videos
in my playlist. I made a playlist
of dogs dancing. JEFF POSNICK: Because that’s
what Dino likes. IKAI LAN: That’s what
Dino likes, not me. Let’s go ahead and translate
this into code. I’m going to open up IntelliJ. This is just the ID I’m using,
and I commented out the code’s already working in case we run
out of time, which we might. We’ll see. We’ll just play it by ear. So I’m going to go ahead and
remove this because this is code I’ve already written. Now, if you download our Java
samples– and these are available at code.google.com/
pe/youtube-api-samples, or you can search for “YouTube API
samples” and find them. JEFF POSNICK: They’re also
in line in the Docs. IKAI LAN: They’re also
in line in the Docs. For Java, it’s better if you go
to the samples because you get a Maven project, and
that way it’ll build everything for you. But once you’ve built it, then
you can go ahead and make changes as needed. So all of this is boilerplate
right here. I’m just prompting the user for
permission, I’m giving an HTTP transport, and
I’m creating this YouTube client object. And this is what we’re going
to be working on. So again, let’s try to
remember what we did. Let’s do a quick review. We went, we listed the channels,
and we got back contentDetails. We set it to my playlist. And then we made an
API call to the Playlist Items playlist. So let’s go ahead and
replicate that here. So the first thing we need
to do is get my channels. So I’m going to go ahead
and write this code. And because I’m using IntelliJ
and this is Java, I get code completion. So you’ll see that all of these
are available in the Java client. Now, what is it that
we want to get? We want channels. So this will be a
channels method. Now, notice that there was
also a capital Channel. This is not what we want,
and I’ll show you what this means later. That’s actually a
package name. Now, let’s go back here
to see what we did. You’ll see that we did channels and the list operation. Now, this is really important. We’re going to channels
and list. Why? Because we have a method
here called list. JEFF POSNICK: That
is always useful. IKAI LAN: It’s very useful. And it takes a string. Now, what goes in that string? Let’s scroll down and see. Well, there’s the one required
bit, which is the part. Let’s go ahead and copy
contentDetails, create a string here. And now we start to set
parameters on it, and this is using the Java builder
pattern. So let’s go ahead and
setMine, and this is kind of a funny thing. We need to use a string true
and not a Boolean true. In some of the languages, you
can just get away with a Boolean instead of a string, but
if a Boolean doesn’t work, just try passing in the string
and see what happens. Now, I think that’s all we need,
so we’re going to go ahead and call execute
on that. And you’ll see that the return
type is a ChannelListResponse. So ChannelListResponse,
channelResponse. We can just do that. Autocomplete saves the day. And the ChannelListResponse is
something that responds to the iterator, it implements
iterator. So what that means
is we can say– I’m sorry. Get Items does. So there’s a Channel, channel,
channelListResponse, and we’re going to call getItems on it
because getItems returns an array because that’s what this
returns down here into JSON. This is a JSON array, and this
translates into more or less a Java list. JEFF POSNICK: Yeah, it’s really
nice that there’s a direct mapping between what you
see in the JSON and what the methods and parameters are
in the Java client library and all the other client
libraries, too. IKAI LAN: Now, just for fun,
let’s go ahead and output. Let me see how I did this. contentDetails we care about. So every one of these objects
has a contentDetails and relatedPlaylists. So what that means is we could
say channel.getConte ntDetails.getRelatedPlaylist. There you go. JEFF POSNICK: There you go. IKAI LAN: And we get back a
related playlist object. So we can say get, and do we
want to get the Likes, Favorites, Uploads,
or WatchHistory? Let’s get back the Likes. Now, we’re just printing this
for now, so let’s go ahead and run this code. JEFF POSNICK: And while it’s
running, I think what Ikai just did is something you’ll
find yourself doing a lot when you’re writing code, going back
and forth between the API Explorer and looking at what
the actual response looks like, and then translating that
into code in whatever language you’re working with. IKAI LAN: And this is too small
on screen, but you’ll see that it output
the channel ID. JEFF POSNICK: Cool. IKAI LAN: So now we’ve
done the first step– JEFF POSNICK: So you must
already be authenticated or authorized. IKAI LAN: That’s a
good point, Jeff. I’m already authorized. That’s because I ran
this yesterday. But really, actually, it saves
the credentials in this weird directory in my home directory,
so I don’t want to clear that right now. But normally what this should do
is this should open up your default browser and ask you,
do you want to give this registered application access
to act on your behalf? And then you could
say yes or no. JEFF POSNICK: But again, this
is another important point that a lot of people struggle
wrapping their heads around. Even though you’re using OAuth2
and the initial time you go through it, you have to
open up a browser and click OK, most of the client libraries
have ways of saving those credentials and
reusing them. That’s what Ikai just
demonstrated. So after you do that once, you
really don’t have to keep going to a browser. It’ll save it for you. IKAI LAN: And in a future show,
we’ll talk about some of the different strategies you can
use to save these tokens for long lived reuse. One question we commonly get
is, well, I’m going to be running this on a
headless box. I don’t want the user to have
to authenticate every time. And you don’t have to because
what you can use, you can use an offline token. You authenticate on your own
laptop, save the token, copy the token onto the server,
and then just use that token over and over. Or you can just have the server
spin up a temporary web page, have you authenticate, and
then quickly take down the web server. And we’ll show you some
strategies about saving the token, really save in a flat
file, save in a database, whatever you like. JEFF POSNICK: There’s
different ways. IKAI LAN: We’re going to show
you some ways in a future show, not this show. JEFF POSNICK: Indeed. IKAI LAN: So now we have this
likesPlaylistId variable. So let’s go ahead and make
the second API call. So the second API call, if you
recall, we’re going back to PlaylistItems, and this
is another list call. So let’s go back to
our YouTube client and let’s do this. YouTube– this is our YouTube client–
.playlistItems. Hey, autocomplete’s great. Now, I’ve already imported all
of these, and that’s why some of these, I didn’t have
to do anything. But usually, your ID will be
smart enough to say, hey, which one of these do
you want to import? It’ll give you a hint,
and you could say, yes, that’s the one. And you’ll see that on top,
it’ll handle the importing everything for you. As long as you have the correct
JAR files in your class path, so that’s
important. And if you use Maven,
those will all be taken care of for you. So a good place to start is from
our samples, download the necessary imports, and you
could just get all this autocomplete for free. So I’m going to go ahead
and call list call. Why list call? Because again, we’re trying to
list the Playlist Items here. Now, let’s scroll down
a little bit. It expects a string and
intuitively, it might be intuitive for you to
pass a Playlist ID. But really, if you remember
what we did here, this corresponds to the
part parameter. So the part parameter was
id,snippet,contentDetails. So let’s pass that in here. Now, let’s set a value. And this is using a builder
pattern, and we’re going to call setPlaylistId. And with this, we’re going
to give it the PlaylistId we just got. We’re going to execute it. JEFF POSNICK: Is contentDetails for this call, too? I thought it was just snippet. IKAI LAN: There’s id, snippet,
and contentDetails according to the Docs. JEFF POSNICK: OK, let’s
see what it– IKAI LAN: Let’s take a look,
see what it actually does. It gives you the videoId. JEFF POSNICK: OK. That’s actually kind
of useful. Fair enough. IKAI LAN: Kind of
useful stuff. So let’s go ahead and do this. We’re going to go ahead and
I think this is called PlaylistItemListResponse. Let’s see.
playlistItemListResponse. And this is right. Now, if I got this type wrong,
like if I said, let’s give it a PlaylistListResponse, this
is going to complain. It’s going to say, hey,
that’s not right. And this is IntelliJ, so it’s
going to fix this for me if I hit Alt-Enter. So Eclipse will do something
similar with Command-1, Control-1, whatever
system you’re on. So now let’s go ahead and let’s
just output all of the titles of these videos. And again, let’s scroll down
to the API Explorer. You’ll see that there’s also an
items array, and where is the title in the items array? It’s in items, it’s in snippet
of each particular item, and it’s right here in title. So let’s go ahead and do that. Let’s do playlistItemResponse,
excuse me, for PlaylistItem, item, and playlistResponse. Item, getItems. There we go. Now, this is strongly typed so
we’ll say, String title is equal to
item.getSnippet.getTitle. I think this will work. That’s not right. [LAUGHS] I don’t want that. I’m going to save this, and I’m
going to go ahead and run this thing again. And there you go. You see that it’s outputting
all of these things. Now, there’s an optimization we
can do here, which is let’s go ahead and do a quick
optimization. Now, we only want one field from
here, so why don’t we go ahead and see what we can
do to filter down the fields that we need? And we talked about this earlier
in the show using the fields editor. Let’s go ahead and click this. Items, let’s expand this. And this was in snippet, so
let’s go to snippet, Close, and Execute it. And there we go. We’ve narrowed it down
significantly. Now, let’s see if we can narrow
it down even more. I wonder if this will
work, /title. And there you go. It works. JEFF POSNICK: I’m going to
tell you, because this is actually a good practice. Be sure that you actually modify
your part parameter to get rid of contentDetails. IKAI LAN: That’s right. JEFF POSNICK: Because it’s
very wasteful to request contentDetails and then
filter it out. IKAI LAN: So let’s remove
id and contentDetails. Oh, well, there you go. Now all we have is this. So let’s go back and
let’s optimize our code a little bit. Let’s see. We only used snippet, so
remove snippet and contentDetails. Now, we still need to
filter this out. So before the Execute, let’s add
a new method, setFields. And the fields, we copy
and paste what we have here in fields. So this is one long line, and
we probably want to format this better, but I’m not going
to do that for this show. We’re going to run this code. And there you go. It still works and it;s
still efficient. Now, we could go ahead and also
do this with the initial playlist request, which
I’m not going to do now for time reasons. JEFF POSNICK: We’re definitely
facing some time constraints right now. IKAI LAN: I am going to show you
guys one last thing before we jump off, which is sometimes,
because these are string values, they’re
easy to get wrong. So let’s say instead
of snippet, you put contentDetails. Now, this is still just
compile time. So when we compile this, this
will still compile because there is still a getSnippet
method on the playlist item, but this won’t work. So let’s go ahead and run this
code and figure out. Let’s see what happens. This is going to blow up. OK, it didn’t want to blow up. Let’s do a better example. JEFF POSNICK: I’m sure you can
make something blow up. IKAI LAN: I can make
this blow up. We’re just going to go ahead
and instead of title, we’re going to say, blah. That’ll make this blow up. There you go, Invalid Field
Selection, or Pointers, or whatever, Title. What’s another field here? Let’s just find a
good example. I’m going to go ahead
and execute this. Let’s go ahead and instead of
asking for title, we’ll ask for channelId. I don’t know why we
would do that. We’re going to get a Null
Pointer exception. There you go. So we’re getting Null. What’s going on here, Jeff? Help me, Jeff. I’m just kidding. Don’t help me. I’ll tell you what’s
going on here. So let’s go ahead and I’m going
to click here to set a debug breakpoint. I’m going to Run and I’m going
to call Debug in my ID. It’s going to do this. Now, we’ve made the
API call already. I wish I could blow this up. Maybe there’s a magnifying
glass thing I can use. Well, just try this out and
you’ll see what I mean. When you go down here into
Debug Window, you’ll see, well, let’s see what’s in this
PlaylistItemResponse variable. Let’s open this up. So it’s got the items array. It’s got the items key. But what’s in here? What’s the value in here? Oh, it’s the snippet. That’s cool. And you can keep expanding down
to see what’s going on. And what you’ll find is that
inside the snippet, there’s only the channel ID, so that’s
not what we want. So let’s go ahead and stop this,
and let’s go ahead and fix this back into title. Now let’s go ahead and
debug this again. Let’s drill down. And we are going to see that if
we keep drilling down, OK, we correctly are getting the
title in here, so we can go ahead and continue. And you should really learn how
to use debugger because you can step over, step over,
step over, step over, step over, and it’ll go ahead
and it’ll output the titles in the video. And this is another good way of
debugging this, and there are many, many good ways
of doing this. And I know I keep saying
one last thing. There’s going to be one
last thing here. And the one last thing here is
just going to be a quick introduction to the fact that
the other client libraries also look very similar. Now, this is some Python code. We don’t have any time to run
this, but you’ll see that once you have a YouTube client, you
call channels, and this maps directly to the code that
we’ve written here. Call channels. We’re calling list. Now, the way we pass the part
is a little bit different. We set mine equal to True,
and again, this does take the Boolean. We call execute. And then we iterate over items,
which isn’t a getItems method, but it’s just items
inside a dictionary. JEFF POSNICK: That’s a little
bit more Pythonic. IKAI LAN: A little bit more
Pythonic, definitely. No types. When I was writing this, I
totally forgot what some of these were called, and that’s
one of the benefits of using something like Java. Or even if you’re using the Go
client library, you can get autocompletion when you’re using
the correct plug-ins for Go Sublime. And you’ll see that I’m just
iterating through this, making another Playlist API
call here, execute. PlaylistId, I’m passing it
this playlist_id which I fetched from this parameter. This code is not as optimized. I didn’t filter it down to just
the methods I needed, but you will likely be doing this
when you’re writing your production code. And once that’s done,
we iterate through this and we print. Well, I’m printing the videoId
here, but you could just as well print a title. And you’ll see that the other
client libraries all do the same thing. So maybe in a future show, let
us know what language you’re using, what language
you care about. I suspect PHP and .NET
are big in there. We’ll talk about doing that
show in the future. So let’s just do a quick recap
of what we talked about today. We did some error handling. And in Python, you would
use PDP, Python Debugger Set Trace. Very cool stuff. And in Python, you can
do a few more things. Because it’s interpreted, you
can actually inject more code while you’re debugging. So quick recap. Definitely use the
API Reference. The API Explorer
is your friend. It’s available not just for
YouTube API, but for many of the popular Google APIs such
as Drive, Analytics, I believe, anything. Anything that you do in the API
Console, you can do in API Explorer for the most part. cURL is also your friend because
I didn’t get to show you guys this today, but one
thing we can do is we can output what we think we are
fetching, and we can run that to cURL to figure out, is my
code doing something weird? Because realistically, these
objects aren’t just going to be in one block of code. You might be doing some
crazy things where you’re passing it around. I personally don’t encourage
this style of coding, but you may have special needs or
you might be using a special HTTP client. So what you want to do,
especially when you’re using a special HTTP client, is to
compare what your HTTP client is doing versus cURL, which is
just unadulterated HTTP. Now, why might you be using
a special HTTP client? Maybe you are batching requests,
and your client will batch them because you’re
on a mobile platform. Maybe you’re using Google App
Engine using your All Fetch. There are many reasons and many
valid reasons why you might want to use a different
HTTP transport. So that’s why it’s always nice
to have cURL as a fall back to see, is it just my API key that
is incorrect, am I just not formatting this correctly,
and so on. So it’s pretty easy
to translate API Reference into code. Now, what gets slightly harder
is debugging issues. Now fortunately, we’ll talk
about this in a future show, but I’m just catching exception
E, but there’s another exception you can catch
here called the Google JSON exception. And in this JSON exception,
you can get details, get cause, and you can match these
against things and figure out what’s going on so you can
respond to errors at run time, and that’s really nice. And there are places where you
might want to do that such as if a user doesn’t have
a YouTube channel. I’ve talked a little bit about
this in my Google I/O talk. I told you this was coming. I’m going to plug it. “Best Practices With Mobile
on YouTube.” Check it out. There is a bit where I talk
about how to handle this case. So that’s all that I have
for you guys today. Here are some useful links. And Jeff, did you have
any questions? JEFF POSNICK: That was super
thorough, so not really any questions here. But if you do have
any questions. IKAI LAN: Are you sure you
don’t have any questions? JEFF POSNICK: I mean, I know
that we’re hiring, and I know that’s what you’re getting at. IKAI LAN: Well, that’s what
we’re getting at here. JEFF POSNICK: I wanted to
plug Stack Overflow as a way to ask questions. Stack Overflow. If people do have questions,
Stack Overflow using the YouTube-API tag is the best
place to ask general programming questions. And then I guess
I’ll feed you. Are we hiring, Ikai? IKAI LAN: We are hiring. Thank you for asking. I didn’t expect you to
ask me that question. Check us out on
developers.google.com/jobs. We are hiring in many sites
around the world, including our headquarters in Mountain
View, New York City, for a variety of positions in
developer relations. That’s not just YouTube, also
Apps Dev Rel– that’s our shorthand– Google+, AdWords, Cloud. There’s so many things
to work on. JEFF POSNICK: A ton of teams and
everybody needs more help. IKAI LAN: Everybody needs help
because there’s never enough– I was going to say there’s
never enough work, and that’s incorrect. JEFF POSNICK: No, that’s
very wrong. IKAI LAN: There’s never enough
time to do all the work that needs to be done, and some of
that work includes creating these shows for you guys. So thank you very much
for setting me up. It was kind of a slow setup,
but thank you. Open for a setup. And we will catch you
guys next time. JEFF POSNICK: See
you, everybody. IKAI LAN: Take it easy, guys. JEFF POSNICK: Cheers. [MUSIC PLAYING]

13 thoughts on “YouTube Developers Live: From API reference to code in minutes”

Leave a Reply

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