Buy the full source code of application here:
https://buy.stripe.com/7sI17oaVr1SodDW2ot
Show More Show Less View Video Transcript
0:00
Uh hello friends today in this tutorial
0:02
we will be building a Google login
0:04
application in PHP using client library
0:07
of Google that is provided for PHP. So
0:09
this is the interface. If I hit the
0:10
Google login button here the link here
0:13
it will be displaying my profile
0:15
picture, email address and the profile
0:18
picture. You can see that. So this is
0:19
the application that we will be building
0:21
here. For this application we are making
0:23
use of this client library specifically
0:25
for PHP. So this is a GitHub link. You
0:29
can see that you can see that this is a
0:31
Google API client library for PHP. With
0:34
the help of this library, you can just
0:36
implement Google authentication system
0:38
in your application. So here just go to
0:40
this link and you can just download the
0:42
zip file and uh if I you will be getting
0:46
this zip file. So if you extract this
0:48
zip file, you will get you will be
0:50
getting this folder client. If you get
0:52
to into this folder, we just want to
0:54
include this vendor folder. Inside this
0:56
vendor folder, we just need to include
0:58
this autoload file here. So just create
1:01
a file here inside your working
1:03
directory. I have already created this
1:05
file which I named as redirect dotphp.
1:08
So I'm using brackets editor here. Let
1:10
me just delete all the code here. So
1:13
first of all, we just need to have our
1:15
PHP tags.
1:17
And if I just save this file here and
1:19
again refresh my project, you will see
1:22
that nothing will be there. You can see
1:23
that it is currently empty. Now we just
1:25
need to include the uh vendor folder
1:29
inside our PHP script. So we will be
1:31
making use of require once. So inside
1:34
this I will just pass here uh
1:37
uh the full path which is uh it is there
1:41
inside client folder. So client
1:47
slash
1:48
it is there in the inside vendor folder
1:51
and /autoload.php.
1:54
So we are just importing that script
1:56
autoload and now we will be initializing
1:59
the configuration.
2:01
So for that first of all we just need to
2:03
create a client ID.
2:07
The second one will be the client secret
2:12
and the third one is the redirect URI.
2:15
So these three things that we we we need
2:18
in order to make this application. So
2:20
for obtaining these three things we just
2:22
need to go to Google developer console
2:24
here. So if you just go to Google and
2:27
type Google developer console
2:30
Google developer
2:32
console and here you just need to create
2:35
a new op project. I have already created
2:37
some projects here. So it is opening. So
2:40
just wait and you just need to create on
2:43
uh just uh click on credentials.
2:47
You can see that just click on
2:49
credentials and then you just need to
2:51
make a new client ID. So it is just
2:55
loading. So just wait. You can see that
2:57
just click on this button and click on
2:59
oath client ID. So click click this
3:02
button. It is a web application. Select
3:04
this. And here you don't want to provide
3:07
authorized JavaScript origins. You just
3:09
want to provide the redirect URI. So the
3:12
redirect URI will be in my case HTTP
3:15
localhost slash the path is google login
3:19
this is a project folder / redirect dot
3:22
php. So this is my file that I am
3:24
creating here inside my brackets editor.
3:27
You can rename this file to anything but
3:29
you but you have to make sure that you
3:31
are providing this value. This needs to
3:34
match. So I will be providing this
3:37
redirect URI and then I will be clicking
3:40
on the create button. So basically this
3:43
is my redirect URI that I have provided
3:45
here and this will give me the client ID
3:47
and client secret. So I will copy this
3:49
client ID here and paste it here
3:54
and also I will copy the client secret
3:57
and paste it here inside client secret.
4:01
And the redirect URI was http that I
4:04
provided there/
4:06
localhost/google
4:09
login/redirect
4:10
dotphp.
4:13
So this needs to match. So now we will
4:16
be creating a simple client request.
4:20
So for this we just need to make client
4:22
object initialize it to new Google
4:28
client.
4:31
So this is the class name
4:36
Google client.
4:38
We just need to initialize this.
4:41
After this initializing this we just
4:43
need to set some things here. Set call a
4:46
method which is set client ID.
4:49
set client ID and we just need to pass
4:52
our client ID which is here client ID.
4:56
Second of all, we just need to set the
4:57
client secret. So set client secret. So
5:01
here we just need to pass our client
5:03
secret.
5:05
And the third thing that we need to do
5:07
is to set our redirect URI.
5:11
So here we will just pass our redirect
5:13
URI. So after setting these three things
5:16
we just need to also add some scopes. So
5:19
basically scope is uh what kind of
5:21
information that you are retrieving. So
5:23
basically we we just need to uh first of
5:26
all add some scope. So in order to add
5:29
scopes we call this method add scope. So
5:32
inside this first of all we just need to
5:34
fetch the email of the person and also
5:37
the profile. So for that we also need to
5:41
pass profile.
5:43
That's it. Now we just need to make a
5:46
very simple if condition.
5:49
Let me just make the if else. So in the
5:51
if condition we will be checking for the
5:52
code which is coming from back back from
5:55
Google. We will use the is set function
5:57
in order to check dollar.get. I will use
6:01
that dollar.get.
6:04
So inside this square brackets I will
6:06
check for the parameter which is code.
6:08
So if this parameter is set in the URL
6:10
then we just need to exchange it for the
6:12
access token. So we will making a new
6:14
variable token and then I will be
6:16
calling this method client fetch access
6:21
fetch access token with o code. So this
6:23
is a function guys. So here we just need
6:25
to pass our code. So which is there
6:28
inside dollar get code. That's it. So
6:32
this will get
6:34
exchange that code which is generated by
6:37
Google and it will give us the access
6:39
token and now we just need to use this
6:42
access token which is generated and we
6:44
just need to set the access token. This
6:46
is a method set access token and we will
6:49
pass the token which is generated.
6:51
That's it.
6:55
We just need to also put a square
6:57
brackets here. So inside this we just
6:58
need to create a property which is
7:00
access token. That's it.
7:04
So after this we just need to get some
7:06
profile information get profile info. So
7:08
this can be easily be grabbed by some
7:11
calling some uh simple method. So first
7:14
of all we just need to create this
7:17
variable Google o and then we will be
7:20
declaring new google service
7:26
o.
7:28
This object you need to create
7:32
o2
7:34
and we just need to pass the client to
7:36
it.
7:39
So after passing this we just need to
7:41
create another function which is which
7:43
will be holding the information for us
7:45
Google account info
7:50
and first of all we will be using the
7:52
Google o and we will be calling a simple
7:55
method which is user info. So inside
7:58
this first of all we just need to get
8:00
some user info. Again we will call get
8:03
method to it.
8:06
So that's it. So now we just need to
8:08
first fetch the email. So for fetching
8:10
the email we will be using Google
8:12
account info.
8:14
email
8:17
and for the name I will be again using
8:21
this Google account info
8:26
name and for the picture we will be
8:29
declaring new variable picture Google
8:31
account info picture
8:34
that's it now we just need to display
8:36
these things so first of all we will
8:40
echo out the
8:44
a heading which will be holding profile
8:47
just a static text. So then we will be
8:51
having our email. So inside another
8:54
heading we will be displaying our uh
8:57
email. Close this heading and uh
9:03
we then want to display our uh name
9:13
And then we just need to display our uh
9:17
picture also. So,
9:21
so now we can also print out the image.
9:23
So, inside image source in single quotes
9:27
we can just pass our picture variable.
9:29
So, close this. And now in the else
9:32
block we just need to create our login
9:33
URL. So, inside this we can just have a
9:36
single a simple href tag.
9:39
So inside this we can just call a simple
9:42
function client create o url
9:46
and after that
9:49
we can just uh close the anchor tag and
9:52
have a simple URL which is goggle Google
9:55
login that's it. So put a semicolon
9:59
and now if I save all these changes and
10:02
if I refresh the page you will see that
10:04
guys there is a Google login anchor tag
10:07
which is there. If I click this, you
10:10
will see that it will redirect me to the
10:12
accounts page. Here I can just select my
10:14
account. If I select this account,
10:17
uh hopefully it will display the email
10:19
address. You can see that email address
10:21
is displayed. This is a display picture
10:24
or name and this is a profile picture.
10:27
So we have successfully implemented
10:29
Google login in PHP using Google client
10:32
library. So thanks very much for
10:34
watching this
