Не удается получить имя после входа в систему с помощью входа в Google

Я использую сервис входа в систему Google для пользователя, чтобы войти в личный кабинет без регистрации для учетной записи.

После успешного входа в систему я могу получить только информацию об их электронной почте, но не имя? В приведенном ниже коде я могу только повторить электронную почту, но имя пусто? Вот мой код, подскажите, пожалуйста, что я сделал не так?

<?php
//error_reporting(0);
//@ini_set('display_errors', 0);
require_once '../social_login/social/google-api-php-  client/src/Google_Client.php';
require_once '../social_login/social/google-api-php-   client/src/contrib/Google_PlusService.php';
require_once '../social_login/social/google-api-php-client/src/contrib/Google_Oauth2Service.php';
include('../social_login/db.php');
include('../configs/dbconnect.php');
ob_start();
session_start();
$client = new Google_Client();
$client->setApplicationName('test');
$client->setClientId($Clientid);
$client->setClientSecret($Client_secret);
$client->setRedirectUri($Redirect_URIs);
$client->setDeveloperKey($apikeys);
$client->setScopes(array(
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/plus.me'
));
$client->setApprovalPrompt('auto');
$client->setAccessType('offline');
$plus = new Google_PlusService($client);

if (isset($_GET['error'])) {
header('Location: /');
exit;
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect          = 'http://' . $_SERVER['HTTP_HOST'] .   $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
$client->revokeToken();
}
if ($client->getAccessToken()) {
if($client->isAccessTokenExpired()) {
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
}
$oauth2Service = new Google_Oauth2Service($client);
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
$_SESSION['token'] = $client->getAccessToken();
$userinfo= $oauth2Service->userinfo->get();
$email = $userinfo['email'];
$username= $userinfo['name'];
echo "My name is: $username<br>";
echo "My email is: $email";
} else {
$authUrl = $client->createAuthUrl();
// print "<a href='$authUrl'>Connect Me!</a>";
}
if (isset($authUrl)) {
// print "<a class='login' href='$authUrl'>Connect Me!</a>";
header('location:' . $authUrl);
}
?>

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …