1
00:00:05,320 --> 00:00:09,820
In the previous video, we added a YouTubePlayerView widget to the layout.

2
00:00:09,820 --> 00:00:14,370
What we need to do now is create and set up a Google API key.

3
00:00:14,370 --> 00:00:20,110
Now the YouTube Android API, which was the jar file that we downloaded from Google's web site, needs this

4
00:00:20,110 --> 00:00:23,880
Google API key and won't work without one.

5
00:00:23,880 --> 00:00:29,770
Now Google use that to limit access, so it lets them keep tabs on individual applications and how much

6
00:00:29,770 --> 00:00:31,840
usage they are using.

7
00:00:31,840 --> 00:00:34,890
Now if an app uses up a lot of resources they can charge you for it.

8
00:00:34,890 --> 00:00:39,880
They do give you a fair amount of free quota though, certainly enough for our application, but it's their

9
00:00:39,880 --> 00:00:43,420
way of limiting access and that's the reason they do it this way.

10
00:00:43,420 --> 00:00:46,250
Now don't worry that you may be charged without knowing about it.

11
00:00:46,250 --> 00:00:51,220
You can't be charged until you've switched to billing and entered your payment details, so that's not something

12
00:00:51,220 --> 00:00:53,380
that can happen without you knowing about it.

13
00:00:53,380 --> 00:00:58,600
You'd also really have to try hard to use up all the free daily quota they give you.

14
00:00:58,600 --> 00:01:03,660
You'd need a lot of devices accessing YouTube with your key, 24 hours a day, to go over.

15
00:01:03,660 --> 00:01:08,110
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

16
00:01:08,110 --> 00:01:17,660
to go about obtaining your Google API key. So the website to go to is console.developers.Google.com.

17
00:01:17,660 --> 00:01:21,970
Now before you can actually use the developer console, you have to sign in with your Google account, which

18
00:01:21,970 --> 00:01:23,810
is probably going to be your gmail address.

19
00:01:23,810 --> 00:01:29,000
It's quite hard to use an Android phone without having a Google account, and Google have introduced two-step

20
00:01:29,000 --> 00:01:31,070
verification for Google accounts now.

21
00:01:31,070 --> 00:01:34,430
So if you've enabled that you may have to enter your verification code.

22
00:01:34,430 --> 00:01:48,990
So I'm just going to log in now.

23
00:01:48,990 --> 00:01:50,970
Now you may get a banner at the top of the screen

24
00:01:50,970 --> 00:01:54,030
like you can see that I've got, offering you a free trial.

25
00:01:54,030 --> 00:01:56,790
You don't actually need one of these to use the API key, so

26
00:01:56,790 --> 00:02:03,670
you can click on dismiss on the top right. The free trial is a free trial of Google's paid service, and

27
00:02:03,670 --> 00:02:06,880
you'd have to be working with a lot of data before you need to do that.

28
00:02:06,880 --> 00:02:12,340
So at this point possibly the screen may be blank, in which case use the three bar menu at top left and

29
00:02:12,340 --> 00:02:16,690
choose the API and Services option, and from there you want library.

30
00:02:16,690 --> 00:02:21,940
But in my case you can see we've got the API and Services menu already selected, and access it from there if

31
00:02:21,940 --> 00:02:30,260
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,

32
00:02:30,260 --> 00:02:37,030
and we can do that from the YouTube API screen, so come over here and click on the YouTube data API link.

33
00:02:37,030 --> 00:02:44,030
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,

34
00:02:44,030 --> 00:02:45,870
and you should see a screen similar to this.

35
00:02:45,870 --> 00:02:51,420
And if by chance you get a completely different looking screen, there is this three bar menu at the

36
00:02:51,420 --> 00:02:55,950
top left. Just choose API services then dashboard like so, but I'm already on there and that's fine.

37
00:02:55,950 --> 00:03:00,210
There's a reminder at the top of the screen as you can see there that you need to create a project

38
00:03:00,210 --> 00:03:01,550
to enable API's.

39
00:03:01,550 --> 00:03:08,180
So let's go ahead and click on the Create Project. Now rather confusingly at this point it asks you to

40
00:03:08,180 --> 00:03:13,680
select a project, but there's a Create button so let's click on that. Alright,

41
00:03:13,680 --> 00:03:19,920
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

42
00:03:19,920 --> 00:03:22,400
you can recognize this project in the console,

43
00:03:22,400 --> 00:03:25,060
and that's because eventually you could have loads of projects in there.

44
00:03:25,060 --> 00:03:29,790
But I'm going to call this one Youtube Test Player, and

45
00:03:29,790 --> 00:03:31,600
I'm going to click on Create.

46
00:03:31,600 --> 00:03:36,990
Now you can say down here what's happened is that Google automatically generate a project ID. Now you

47
00:03:36,990 --> 00:03:39,510
can change that but there's little point.

48
00:03:39,510 --> 00:03:44,760
It's used internally to make sure all projects are globally unique, and you'll be using the project name

49
00:03:44,760 --> 00:03:45,960
to work as a project,

50
00:03:45,960 --> 00:03:52,210
so leave that ID that Google generate as is, and click on Create. So there's a bit of a delay now

51
00:03:52,210 --> 00:03:58,670
while the project gets created, and then we should be moved back to the dashboard.

52
00:03:58,670 --> 00:04:03,680
Alright so the dashboard describes two ways we might want to use our credentials

53
00:04:03,680 --> 00:04:06,140
with this API, as you can see on the screen there.

54
00:04:06,140 --> 00:04:11,420
And if we want to access user data, so videos that aren't marked as public in other words, then we need to

55
00:04:11,420 --> 00:04:14,000
create an OAuth cloned ID.

56
00:04:14,000 --> 00:04:20,269
But we're going to be playing public videos so an API key is sufficient. Now by the way you can create an

57
00:04:20,269 --> 00:04:22,310
OAuth client ID later,

58
00:04:22,310 --> 00:04:27,500
if you want to access private videos. You'd use OAuth authentication if you were creating an app that

59
00:04:27,500 --> 00:04:32,030
allowed the user to sign in on their own account, to access protected content.

60
00:04:32,030 --> 00:04:33,290
But we're not going to do that.

61
00:04:33,290 --> 00:04:35,450
We'll only be playing public videos,

62
00:04:35,450 --> 00:04:37,730
so therefore an API key is fine.

63
00:04:37,730 --> 00:04:40,970
And this screen's just information to explain the different credentials.

64
00:04:40,970 --> 00:04:46,580
You can click the Learn more links if you want to learn more information on each one, but to generate

65
00:04:46,580 --> 00:04:51,680
the API key, click on the Enable link at the top of the screen, this one here.

66
00:04:51,680 --> 00:04:58,130
And once the screen opens we should have access to the quota and that can take a while to actually finish,

67
00:04:58,130 --> 00:05:02,360
so we'll just give it a moment to complete. Alright,

68
00:05:02,360 --> 00:05:07,610
so the quotas tab, now that that has been generated, lets you see how much use you have, before you must

69
00:05:07,610 --> 00:05:09,200
sign up and start paying.

70
00:05:09,200 --> 00:05:14,380
Now if your app's going to make more than a million queries per day, or more than 300000 queries per user in

71
00:05:14,380 --> 00:05:17,600
100 seconds, then it's time to start paying.

72
00:05:17,600 --> 00:05:21,960
But you've probably got a lot of users of your app by then if you're exceeding those limits.

73
00:05:21,960 --> 00:05:26,420
Now remember that you haven't signed up for billing at this point, so you won't be charged in that case

74
00:05:26,420 --> 00:05:28,520
even if you do manage to hit those limits.

75
00:05:28,520 --> 00:05:33,590
What will happen is the app will just stop working until midnight, when the quotas are actually reset. So again we can

76
00:05:33,590 --> 00:05:37,180
click on quotas and see what those limits are.

77
00:05:37,180 --> 00:05:39,970
In this case we need to to use this API.

78
00:05:39,970 --> 00:05:44,230
We need to come over here and click on Create credentials.

79
00:05:44,230 --> 00:05:48,320
By the way you can get to the same place by choosing the Credentials link over on the left hand side

80
00:05:48,320 --> 00:05:50,970
of the screen, the left pane.

81
00:05:50,970 --> 00:05:53,160
Now this is a fairly straightforward process.

82
00:05:53,160 --> 00:05:58,140
We've already selected the YouTube data API, so that appears in the first dropdown automatically as you

83
00:05:58,140 --> 00:05:59,750
can see here.

84
00:05:59,750 --> 00:06:03,270
Now we need to tell it what kind of access we want our key for,

85
00:06:03,270 --> 00:06:05,480
so we want to choose Android,

86
00:06:05,480 --> 00:06:08,250
this second option here, so click on Android.

87
00:06:08,250 --> 00:06:13,350
Now you need a different key for each type of access, so if you were also creating a web app for example,

88
00:06:13,350 --> 00:06:16,020
you'd have to generate another key to use it for that.

89
00:06:16,020 --> 00:06:22,720
Now we want access to public data, so we're going to click on and select that radio button, and come over here

90
00:06:22,720 --> 00:06:28,970
then click on the What credentials do I need button, to go to Step 2. So we come over here to Restrict key,

91
00:06:28,970 --> 00:06:33,170
and that brings us up to another screen there now, and again you can't see a lot of his data.

92
00:06:33,170 --> 00:06:39,760
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

93
00:06:39,760 --> 00:06:43,120
restrict the key, to prevent someone else using it.

94
00:06:43,120 --> 00:06:49,340
So we're going to restrict the key to Android apps, and come down here and select Android apps.

95
00:06:49,340 --> 00:06:51,770
Now by restricting the use of our key to our app,

96
00:06:51,770 --> 00:06:54,920
it becomes very difficult for anyone else to use it.

97
00:06:54,920 --> 00:06:59,140
They'd have to create an Android app with the same name as ours to start with,

98
00:06:59,140 --> 00:07:04,170
and Google insists that all the apps on the playstore have a unique name, and that's why we use that Reverse

99
00:07:04,170 --> 00:07:06,810
domain when creating our projects.

100
00:07:06,810 --> 00:07:10,610
Now by linking the key to the apps SHA1 fingerprint,

101
00:07:10,610 --> 00:07:15,020
it becomes almost impossible for someone else to use our key.

102
00:07:15,020 --> 00:07:21,610
So what it now asks for is the unique SHA1 certificate fingerprint from your app. Now to cut a long story

103
00:07:21,610 --> 00:07:26,500
short, this is just a way of linking what is unique about your application and giving that to Google,

104
00:07:26,500 --> 00:07:28,390
so that they can link the two together.

105
00:07:28,390 --> 00:07:33,820
Actually the SHA1 fingerprint's tied to your Android Studio keystore, but this is only a debug version

106
00:07:33,820 --> 00:07:36,790
anyway, so you don't have to worry too much about the details.

107
00:07:36,790 --> 00:07:40,840
I will be going into more detail about this in a later section, when we look at deploying an app to

108
00:07:40,840 --> 00:07:46,520
the Google playstore, but at the moment the issue is creating the SHA1 certificate fingerprint. Now

109
00:07:46,520 --> 00:07:51,820
Google are showing us the way to do this, on the screen here down the bottom, but you have to start a command

110
00:07:51,820 --> 00:07:56,300
prompt or terminal session and find out where your keystore is and it's a bit convoluted.

111
00:07:56,300 --> 00:08:01,030
Now if you're using Android Studio though, which we are of course, there's a much easier way.

112
00:08:01,030 --> 00:08:07,720
So what I'm going to do is go back to Android Studio briefly. Now there's this Gradle tab over here, at the

113
00:08:07,720 --> 00:08:15,190
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 

114
00:08:15,190 --> 00:08:16,870
Gradle, to open it up there.

115
00:08:16,870 --> 00:08:21,190
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

116
00:08:21,190 --> 00:08:25,580
right as you can see. Now we click on the refresh button just to make sure that everything's up to date,

117
00:08:25,580 --> 00:08:30,700
and you can see it's refreshing the Gradle, the YouTubePlayer Gradle project now. I'll give that a

118
00:08:30,700 --> 00:08:35,429
moment to complete. OK you can see that's now completed.

119
00:08:35,429 --> 00:08:37,419
So we then want to expand the project name,

120
00:08:37,419 --> 00:08:40,630
so in this case, YouTubePlayer, then click on that to expand it.

121
00:08:40,630 --> 00:08:45,310
Then we want to come down and expand tasks which is already expanded as you can see here, then Android,

122
00:08:45,310 --> 00:08:46,570
so expand that.

123
00:08:46,570 --> 00:08:54,850
So that's basically saying that the signingReport over here is now available, so we double click that.

124
00:08:54,850 --> 00:09:00,340
You saw that the signingReport pane opens up and at the bottom of the window on the run tab.

125
00:09:00,340 --> 00:09:06,010
So it starts off by showing the various scripts and how long they took to execute, and that can be useful

126
00:09:06,010 --> 00:09:10,630
with large projects if everything's taking too long to build, so let's just see where the time's been

127
00:09:10,630 --> 00:09:14,110
spent. But we're not actually interested in that at this stage,

128
00:09:14,110 --> 00:09:20,740
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,

129
00:09:20,740 --> 00:09:27,950
this one here. So we want to use the SHA1 key and not any other key, because when we scroll up we can see

130
00:09:27,950 --> 00:09:29,990
there's an MD5 key there as well.

131
00:09:29,990 --> 00:09:33,120
But the one we want is the SHA1 key.

132
00:09:33,120 --> 00:09:37,820
So we're going to copy the entire key to the clipboard. So we want to copy that,

133
00:09:37,820 --> 00:09:44,690
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.

134
00:09:44,690 --> 00:09:45,590
Once we've copied that,

135
00:09:45,590 --> 00:09:48,590
we want to go back to the browser,

136
00:09:48,590 --> 00:09:54,810
and what we need to do now, is click on the plus here, plus Add package name and fingerprint.

137
00:09:54,810 --> 00:09:58,710
Alright, so we need to enter our app's package name and SHA1 fingerprint here.

138
00:09:58,710 --> 00:10:06,500
Now we've got the SHA1 in the keyboard, in the clipboard, so let's just paste that into the second box, like so.

139
00:10:06,500 --> 00:10:10,040
Now rather than typing in the package name and risking making a mistake,

140
00:10:10,040 --> 00:10:13,310
let's go back to Android Studio. Now

141
00:10:13,310 --> 00:10:18,500
the package name appears at the top of our code file, so if we open up our Main Activity.kt you

142
00:10:18,500 --> 00:10:20,270
can see the package name there. Now

143
00:10:20,270 --> 00:10:24,290
I've got Kotlin code here but the package name's identical for Java or Kotlin.

144
00:10:24,290 --> 00:10:27,350
You want to copy the name to your clipboard, ignoring the word package at the start,

145
00:10:27,350 --> 00:10:33,470
so just this part here, academy.learnprogramming.youtubeplayer in my particular case. So we'll copy that, switch

146
00:10:33,470 --> 00:10:35,090
back to the browser again.

147
00:10:35,090 --> 00:10:38,460
And what we're going to do is paste this into the package name box,

148
00:10:38,460 --> 00:10:41,220
and you can see we've got the full name there now.

149
00:10:41,220 --> 00:10:45,220
Now you can associate test keys as well if you like, but that's not something that we're going to be doing

150
00:10:45,220 --> 00:10:46,480
at this point in time.

151
00:10:46,480 --> 00:10:50,140
So now we can actually go ahead and click on Save.

152
00:10:50,140 --> 00:10:55,580
Now once you understand how to create an API key, in this case for YouTube, it's a very similar process

153
00:10:55,580 --> 00:10:58,400
to do this for any other Google service.

154
00:10:58,400 --> 00:11:03,500
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

155
00:11:03,500 --> 00:11:09,560
select the key, and copy that into the clipboard. Now the quickest way to do that is to double click

156
00:11:09,560 --> 00:11:12,500
on it rather than trying to select it without including spaces.

157
00:11:12,500 --> 00:11:16,520
So just double click it, but you want to make sure that the whole key's selected, so you may have to do

158
00:11:16,520 --> 00:11:20,530
a bit of copying to make sure its selected like that, or selecting to make sure its all selected.

159
00:11:20,530 --> 00:11:24,440
Ive got the whole thing selected now. Now we're done in here,

160
00:11:24,440 --> 00:11:29,400
so at this point we can close the browser, and sign out first if you're using a shared computer, using the icon

161
00:11:29,400 --> 00:11:33,410
at the top right that looks like a person. It may have your photo in it of course if you've provided

162
00:11:33,410 --> 00:11:38,120
one with your Google account. But let's end the video here. In the next one we'll start updating the

163
00:11:38,120 --> 00:11:40,650
code in Android Studio, to use this key.

164
00:11:40,650 --> 00:11:41,960
So I'll see you in the next video.

