JEFFREY POSNICK: Hey everybody, this
is Jeff Posnick from the YouTube Developer Relations Team.
We're continuing a series of videos talking about migration
that developers might have going from Version 2 of the YouTube
Data API, to Version 3.
We strongly encourage all developers
to start that migration if they haven't done so yet.
Today we're going to be talking about a way of subscribing
to a user's channel and show how that's
changed between Version 2 and Version 3 of the API.
So let's first talk a little bit about how you would subscribe
to a given YouTube channel in Version 2 of the API.
I just have a page in the documentation
up here, to just give an overview
for folks who aren't familiar.
This is the actual RESTful API call that you use in Version 2.
Everything, again, is Atom XML base,
so you have this pretty big XML document
that you would pass in, in a post request that's
been authenticated, obviously.
And this will end up subscribing to a given channel.
So that does work, but let's talk
about how you do that in the more modern Version
3 of the YouTube Data API.
So, to start, in Version 3 we operate
on channel IDs, rather than legacy user names.
And this is particularly important,
because there are not legacy user names for every given
YouTube channel.
There are channels out there that
just have channel IDs associated with them.
So using a channel ID consistently
throughout Version 3 of the API means
you can deal the exact same way, with any type of channel,
even using these newer channels that
don't have legacy user names.
But that also means that if we do have a channel,
and we know what the legacy user name is,
we need to first translate that legacy user name into a channel
ID.
So let's do that using the V3 API Call youtube.channels.list.
I'm going to show an example of making this API
call from within the API Explorer, which is just
a web-based harness for making API calls,
and just kind of simplifies things.
It's a great way to play around with the API
before you actually start writing code
using our client libraries.
If you wanted to experiment with what different API parameters
do, I really highly encourage that you check it out.
So in this particular case, we want
to translate a user name into a Channel ID.
In V3 there's this concept of a part parameter,
that most of our operations take.
In this particular case, I want the ID part returned.
Think of this as a way of limiting the response that you
get back from the API, so that it only
contains the information that you specifically asked for.
Instead of receiving a really large document that
has all the details about a channel, I'm saying here
all I want back is the ID.
So this is going to result in fast response.
It's great for contexts where you
might have limited bandwidth, or just don't
want to deal with processing a whole big response document.
So I'm going to say I want the ID,
and the channel that I want the ID for is Google Developers.
It's kind of our standard subscription channel.
I'm going to go down here, and I'm going to click Execute.
This is making the V3 API Call for me.
And I'm going to get back this very small response in JSON,
and it contains this ID value.
So let me copy that, and I'm going
to use it in my next API Call.
The actual way of concerning a subscription in V3 of the API
is with a youtube.subscriptions.insertcall.
I have here the documentation for that particular API call.
If you haven't looked through the V3 API documentation yet,
I'd recommend that you pour through it a little bit.
It looks a little daunting in that
there's a whole bunch of different methods listed over
here, on the left hand side that you navigate through.
But I think you'll find that all of the methods
follow a very, very consistent interface,
and naming conventions that are used throughout.
Once you get familiar with making API calls,
against one particular service, using methods
and other services should become second nature.
So hopefully you'll find it's a really consistent API.
So, again, in this particular case
I have the documentation for the Subscriptions Insert Method
open, and you'll learn a little bit more
about what's different parameters are.
And you'll learn about what you can set in the request body
when you're making your YouTube API V3 request.
In this particular case, it's saying
that I need to specify something for snippet.resource ID.
One of the other nice things about the V3 documentation
is that we have these live snippets of code,
that demonstrate how to make the API call,
using the client library selected over here.
So Java, PHP, Python, and Ruby for this particular API call.
So if you were doing pipeline code, for instance,
you could pretty much just copy this, and do it straightaway
without having to change too much.
But I want to show how you would make
that API call in a little bit more of an agnostic way.
So we have, pretty much, the equivalent
of this other page, this API explorer,
embedded inline in our documentation,
at the very bottom of this page.
And it's going to allow me to make this API call.
The other nice thing that allows me to do,
is go through the OAuth2 flow, for authorizing access
to my particular channel.
It has to know who I am before it'll
let me subscribe to a given channel.
So this simplifies things for testing purposes.
I'm just going click Authorize down here.
When you're running your own code,
you're going to want make use of your own OAuth2 registration.
We have some other training material
out there that talks you through the whole process of getting up
to speed with OAauth2.
But for these purposes, we could just test, using that.
I'm going to put in part equals snippet,
because I know, from reading up here,
that I need to set a value for snippet.resource ID.
So this is my way, even though this is doing an insert call,
it's saying what fields I'm going
to be setting in the request body.
And this API explorer has a really nice interface over here
for choosing what I'm sending in the request body.
It's a way of programmatically building up
my request body for me.
Again, if you're writing real code,
you're gonna probably be using a client library.
There's gonna be a equivalents of doing
this in each client library.
And refer to our documentation.
You can also refer to github.com/youtube,
where we have all these examples that are in-line in the docs.
We have a standalone version there.
So check that out.
But for illustrative purposes, I'm
going to just do this over here.
So I'm setting Snippet Resource ID, Channel ID,
and I'm going to be pasting in the channel ID
that I got from the previous call.
And I'm going to click Execute.
At this point, I will have a subscription to that channel.
So that's great.
It just gives me some information
about the channel that I just subscribed
to, over here in the response.
And it looks like everything worked as expected.
So that gives you an example of how you could make a YouTube
Data API Version 3 subscription insertion.
Once you get familiar with that basic way of doing subscription
insertions, you'll find that things
like inserting a new playlist or adding items to a playlist
or doing a number of other operations,
all follow the very same principle.
Hopefully it'll be a straightforward migration
for you, moving your code from V2 to the V3 equivalent.
Thanks very much taking the time to watch this.
Again, I'm Jeff Posnick from the YouTube Developer Relations
Team, and happy migrating.