In the previous video, we added a YouTubePlayerView widget to the layout.
What we need to do now is create and set up a Google API key.
Now the YouTube Android API, which was the jar file that we downloaded from Google's web site, needs this
Google API key and won't work without one.
Now Google use that to limit access, so it lets them keep tabs on individual applications and how much
usage they are using.
Now if an app uses up a lot of resources they can charge you for it.
They do give you a fair amount of free quota though, certainly enough for our application, but it's their
way of limiting access and that's the reason they do it this way.
Now don't worry that you may be charged without knowing about it.
You can't be charged until you've switched to billing and entered your payment details, so that's not something
that can happen without you knowing about it.
You'd also really have to try hard to use up all the free daily quota they give you.
You'd need a lot of devices accessing YouTube with your key, 24 hours a day, to go over.
Alright so I'm in my browser, and what I'm going to do now is go to the Google website and show you how
to go about obtaining your Google API key. So the website to go to is console.developers.Google.com.
Now before you can actually use the developer console, you have to sign in with your Google account, which
is probably going to be your gmail address.
It's quite hard to use an Android phone without having a Google account, and Google have introduced two-step
verification for Google accounts now.
So if you've enabled that you may have to enter your verification code.
So I'm just going to log in now.
Now you may get a banner at the top of the screen
like you can see that I've got, offering you a free trial.
You don't actually need one of these to use the API key, so
you can click on dismiss on the top right. The free trial is a free trial of Google's paid service, and
you'd have to be working with a lot of data before you need to do that.
So at this point possibly the screen may be blank, in which case use the three bar menu at top left and
choose the API and Services option, and from there you want library.
But in my case you can see we've got the API and Services menu already selected, and access it from there if
need be, and we'll click on library, and in my case it's already selected. What we want to do now is create a new project,
and we can do that from the YouTube API screen, so come over here and click on the YouTube data API link.
And we've got these updates to terms of service, so I want to click on yes and accept, we accept the terms of service,
and you should see a screen similar to this.
And if by chance you get a completely different looking screen, there is this three bar menu at the
top left. Just choose API services then dashboard like so, but I'm already on there and that's fine.
There's a reminder at the top of the screen as you can see there that you need to create a project
to enable API's.
So let's go ahead and click on the Create Project. Now rather confusingly at this point it asks you to
select a project, but there's a Create button so let's click on that. Alright,
now on this screen now we've got a, it's asking you for a project name, and that name's for your own use so that
you can recognize this project in the console,
and that's because eventually you could have loads of projects in there.
But I'm going to call this one Youtube Test Player, and
I'm going to click on Create.
Now you can say down here what's happened is that Google automatically generate a project ID. Now you
can change that but there's little point.
It's used internally to make sure all projects are globally unique, and you'll be using the project name
to work as a project,
so leave that ID that Google generate as is, and click on Create. So there's a bit of a delay now
while the project gets created, and then we should be moved back to the dashboard.
Alright so the dashboard describes two ways we might want to use our credentials
with this API, as you can see on the screen there.
And if we want to access user data, so videos that aren't marked as public in other words, then we need to
create an OAuth cloned ID.
But we're going to be playing public videos so an API key is sufficient. Now by the way you can create an
OAuth client ID later,
if you want to access private videos. You'd use OAuth authentication if you were creating an app that
allowed the user to sign in on their own account, to access protected content.
But we're not going to do that.
We'll only be playing public videos,
so therefore an API key is fine.
And this screen's just information to explain the different credentials.
You can click the Learn more links if you want to learn more information on each one, but to generate
the API key, click on the Enable link at the top of the screen, this one here.
And once the screen opens we should have access to the quota and that can take a while to actually finish,
so we'll just give it a moment to complete. Alright,
so the quotas tab, now that that has been generated, lets you see how much use you have, before you must
sign up and start paying.
Now if your app's going to make more than a million queries per day, or more than 300000 queries per user in
100 seconds, then it's time to start paying.
But you've probably got a lot of users of your app by then if you're exceeding those limits.
Now remember that you haven't signed up for billing at this point, so you won't be charged in that case
even if you do manage to hit those limits.
What will happen is the app will just stop working until midnight, when the quotas are actually reset. So again we can
click on quotas and see what those limits are.
In this case we need to to use this API.
We need to come over here and click on Create credentials.
By the way you can get to the same place by choosing the Credentials link over on the left hand side
of the screen, the left pane.
Now this is a fairly straightforward process.
We've already selected the YouTube data API, so that appears in the first dropdown automatically as you
can see here.
Now we need to tell it what kind of access we want our key for,
so we want to choose Android,
this second option here, so click on Android.
Now you need a different key for each type of access, so if you were also creating a web app for example,
you'd have to generate another key to use it for that.
Now we want access to public data, so we're going to click on and select that radio button, and come over here
then click on the What credentials do I need button, to go to Step 2. So we come over here to Restrict key,
and that brings us up to another screen there now, and again you can't see a lot of his data.
Now at the moment the way it's set up, the API key can be used by any tyoe of app, but it's recommended that you
restrict the key, to prevent someone else using it.
So we're going to restrict the key to Android apps, and come down here and select Android apps.
Now by restricting the use of our key to our app,
it becomes very difficult for anyone else to use it.
They'd have to create an Android app with the same name as ours to start with,
and Google insists that all the apps on the playstore have a unique name, and that's why we use that Reverse
domain when creating our projects.
Now by linking the key to the apps SHA1 fingerprint,
it becomes almost impossible for someone else to use our key.
So what it now asks for is the unique SHA1 certificate fingerprint from your app. Now to cut a long story
short, this is just a way of linking what is unique about your application and giving that to Google,
so that they can link the two together.
Actually the SHA1 fingerprint's tied to your Android Studio keystore, but this is only a debug version
anyway, so you don't have to worry too much about the details.
I will be going into more detail about this in a later section, when we look at deploying an app to
the Google playstore, but at the moment the issue is creating the SHA1 certificate fingerprint. Now
Google are showing us the way to do this, on the screen here down the bottom, but you have to start a command
prompt or terminal session and find out where your keystore is and it's a bit convoluted.
Now if you're using Android Studio though, which we are of course, there's a much easier way.
So what I'm going to do is go back to Android Studio briefly. Now there's this Gradle tab over here, at the
top of the right hand margin, so click on that to open it, and if you can't see it click on View, Tool Windows and
Gradle, to open it up there.
You didn't see it for some reason but you can see in my case it's selected there, but in my case it's actually open now on the
right as you can see. Now we click on the refresh button just to make sure that everything's up to date,
and you can see it's refreshing the Gradle, the YouTubePlayer Gradle project now. I'll give that a
moment to complete. OK you can see that's now completed.
So we then want to expand the project name,
so in this case, YouTubePlayer, then click on that to expand it.
Then we want to come down and expand tasks which is already expanded as you can see here, then Android,
so expand that.
So that's basically saying that the signingReport over here is now available, so we double click that.
You saw that the signingReport pane opens up and at the bottom of the window on the run tab.
So it starts off by showing the various scripts and how long they took to execute, and that can be useful
with large projects if everything's taking too long to build, so let's just see where the time's been
spent. But we're not actually interested in that at this stage,
so we want to use the toggle button on the left hand margin, next to the green Run button to toggle with the text mode,
this one here. So we want to use the SHA1 key and not any other key, because when we scroll up we can see
there's an MD5 key there as well.
But the one we want is the SHA1 key.
So we're going to copy the entire key to the clipboard. So we want to copy that,
and we only want the key, we don't want this SHA1 colon heading at the start, or a space, so it's exactly just the key.
Once we've copied that,
we want to go back to the browser,
and what we need to do now, is click on the plus here, plus Add package name and fingerprint.
Alright, so we need to enter our app's package name and SHA1 fingerprint here.
Now we've got the SHA1 in the keyboard, in the clipboard, so let's just paste that into the second box, like so.
Now rather than typing in the package name and risking making a mistake,
let's go back to Android Studio. Now
the package name appears at the top of our code file, so if we open up our Main Activity.kt you
can see the package name there. Now
I've got Kotlin code here but the package name's identical for Java or Kotlin.
You want to copy the name to your clipboard, ignoring the word package at the start,
so just this part here, academy.learnprogramming.youtubeplayer in my particular case. So we'll copy that, switch
back to the browser again.
And what we're going to do is paste this into the package name box,
and you can see we've got the full name there now.
Now you can associate test keys as well if you like, but that's not something that we're going to be doing
at this point in time.
So now we can actually go ahead and click on Save.
Now once you understand how to create an API key, in this case for YouTube, it's a very similar process
to do this for any other Google service.
Alright so we're going to need that key to use it in our app, so what we want to do is come over here and
select the key, and copy that into the clipboard. Now the quickest way to do that is to double click
on it rather than trying to select it without including spaces.
So just double click it, but you want to make sure that the whole key's selected, so you may have to do
a bit of copying to make sure its selected like that, or selecting to make sure its all selected.
Ive got the whole thing selected now. Now we're done in here,
so at this point we can close the browser, and sign out first if you're using a shared computer, using the icon
at the top right that looks like a person. It may have your photo in it of course if you've provided
one with your Google account. But let's end the video here. In the next one we'll start updating the
code in Android Studio, to use this key.
So I'll see you in the next video.