<code><?php
require_once
__DIR__ .
'/g/vendor/autoload.php'
;
session_start();
$client
=
new
Google_Client();
$client
->setAuthConfigFile(
'client_secrets.json'
);
$client
->setRedirectUri(
'http://www.euask.com/oauth/google.php'
);
$client
->setScopes(
array
(
'https://www.googleapis.com/auth/userinfo.email'
,
'https://www.googleapis.com/auth/plus.login'
,
'https://www.googleapis.com/auth/plus.me'
));
$plus
=
new
Google_Service_Oauth2(
$client
);
if
(isset(
$_REQUEST
[
'logout'
])) {
session_unset();
}
if
(isset(
$_GET
[
'code'
])) {
$client
->authenticate(
$_GET
[
'code'
]);
$_SESSION
[
'access_token'
] =
$client
->getAccessToken();
$redirect
=
'http://www.euask.com/oauth/google.php'
;
header(
'Location: '
. filter_var(
$redirect
, FILTER_SANITIZE_URL));
}
if
(isset(
$_SESSION
[
'access_token'
]) &&
$_SESSION
[
'access_token'
]) {
$client
->setAccessToken(
$_SESSION
[
'access_token'
]);
$me
=
$plus
->userinfo->get();
$id
=
$me
[
'id'
];
$name
=
$me
[
'givenName'
];
$email
=
$me
[
'email'
];
$profile_image_url
=
$me
[
'picture'
];
}
else
{
$auth_url
=
$client
->createAuthUrl();
}
?>
<p>
<?php
if
(isset(
$auth_url
)) {
echo
"<a href='"
.filter_var(
$auth_url
, FILTER_SANITIZE_URL).
"'>Google login</a>"
;
}
else
{
print
"Name: {$name} <br />"
;
print
"Email: {$email} <br />"
;
print
"Image: {$profile_image_url} <br />"
;
echo
"<a href='?logout'>Sign out</a>"
;
}
?>
</p></code>