In this tutorial, we'll be talking about how to run the
Java sample code.
To run the YouTube Java samples, we'll need the
following--
Java 1.6, Apache Maven, access to a command
line, and a text editor.
In a future segment, we'll walk through the process of
running and modifying the Java samples from an IDE such as
Eclipse or IntelliJ IDE.
For this tutorial, we'll be running our samples from the
command line.
The first thing I want to do is download the code from this
site on Project Hosting.
Browse to code.google.com/ p/youtube-api-samples.
You can also search for "YouTube-api-samples" in your
favorite search engine and click on
the link to the samples.
Click on the download link.
This will take us to the page where you can download the
code samples.
We'll save the zip file to our local disk.
Now let's go ahead and unzip the zip file.
It's now in my Downloads directory.
I'm going to go ahead and browse to this directory from
the command line.
I'm opening this up in Sublime Text, but you can use any text
editor that works for you.
We're running two samples.
The first sample we're going to run is going
to be a search sample.
Browse to search.java in the YouTube
command line search project.
This is under Samples, Java, YouTube Command Line Search,
Source, Main, Java, Com, Google, API,
Services, Sample, YouTube.
There's a whole hierarchy.
I won't get too much into it.
But you want to go all the way to the bottom of
the directory structure.
Open up the source code.
We'll see that this file makes a reference to
YouTube.properties file, which it loads when it starts up in
the main function.
It then uses the developer key from this file.
This allows the specify developer key and change
developer key without having to recompile the project.
Let's browse to this YouTube.properties file.
Right now this has a placeholder.
We'll need to copy the API key into this file so we can run
the sample.
Let's browse back to the API console.
If you've seen the first tutorial about how to do this,
it will explain how to create this API key.
Let's copy the API key.
Switching back, let's go ahead and paste
over the place holder.
Now let's save the file.
We're now ready to compile and run this code.
Open up the command line again.
We're browsing the samples, and we're
drilling all the way down.
We're now going to use the Maven tool to build and run
this product.
Type in mvn to make sure you have a working Maven
installation.
Now let's go ahead and type mvn compile.
This will download any dependencies and compile the
project for us.
We're looking for "build success." This ran fairly
quickly on my machine, but that's because I've run this
sample before.
Your very first time running Maven Compile on any Google
API project will take a little bit longer, because you need
to download the dependencies.
Now let's go ahead and run the code.
To do this, type mvn exec:java.
This will run the correct project.
In this case, I'm going to search for the term "dogs
dancing."
This will output a list of videos that
match my search query.
I can now go back to search.java and make changes
to learn about the API, recompile using mvn compile,
and run using mvn exec:java.
The second sample we're going to run is going to
authenticate and authorize using OAuth 2.0.
We're going to run the My Upload Sample.
Browse to the My Upload Sample in your YouTube Samples
directory, and drill all the way down, as we
did in the last step.
This one loads a different configuration file called
client_secrets.json.
Let's go ahead and open that file.
This makes use of client ID and the client secret, which
we created in a previous tutorial.
Let's go back to the API console and copy and paste
these values.
We're no longer using simple API access.
We want to use the client ID and client secret.
Let's go ahead copy the client ID, paste it into the value
for client ID in our client_secrets.json.
Let's go ahead and copy client secret.
We'll paste that in as well.
Let's go ahead and save this file.
There's one more thing we need to do.
When we run the sample, it's going to
create a local server.
This is going to accept the browser redirect.
I'll show you what this means in a second.
For now, what we need to do is go back to the API console,
and we need to set a redirect URI for our client ID.
I already have this set up, but you probably won't have
this, so you want to go ahead and click Edit Settings and
add the value http://localhost:8080/Callback.
Click update.
I'll show you what this means.
Browse to your command line again.
Let's enter the My Uploads directory.
Again, we compile using mvn compile, Maven Compile.
It goes ahead and installs all of our dependencies, and it
compiles the code.
Now let's run the code with mvn exec:java.
Notice that it automatically redirects my browser and it
closes it if you authorized in the past.
If I hadn't authorized this in the past, I would be presented
with a screen that says, this application is requesting
permission to your YouTube account.
You'll need to click Allow Access for this to work.
Going back to the command line, we'll see that this has
appropriately listed out the videos that I've uploaded.
Going back to our text editor, we can see that we can make
changes to myuploads.java, save them, and, using Maven
Compile and mvn exec:java, learn about the API and OAuth
2.0 authorized API calls.
That concludes this tutorial.
In a future segment, we'll talk about how to integrate
with an IDE.