2
我是新来的php和google api,我试图在用户使用Google+进行登录时获取电子邮件地址,使用用户信息$加功能>以人为>获取(“我”),但是当我试图把它失败,此错误的电子邮件地址:当用户使用Google Oauth2和people.me进行身份验证时如何获取电子邮件地址
PHP公告:未定义指数:值
这里是我的代码:
<?php
session_start();
set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] . "/src");
require_once 'Google/Client.php';
require_once 'Google/Service/Plus.php';
$client_id = 'XXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com';
$client_secret = 'XXXXXXXXXXXXXXXXXXX';
$redirect_uri = 'http://www.XXXXXXXXXXXXXX.com/pruebas.php';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/userinfo.profile");
$client->addScope("https://www.googleapis.com/auth/userinfo.email");
$plus = new Google_Service_Plus($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Pruebas</title></head>
<body>
<?php if (isset($authUrl)) {
print "<a class='login' href='$authUrl'><img src='logogoo/Red-signin-Medium-base-32dp.png'></a>";
} else {
print "<a class='logout' href='pruebas.php?logout'>Cerrar:</a>";
}
if (isset($_SESSION['access_token'])) {
$me = $plus->people->get("me");
print "<br>ID: {$me['id']}\n<br>";
print "Display Name: {$me['displayName']}\n<br>";
print "Image Url: {$me['image']['url']}\n<br>";
print "Url: {$me['url']}\n<br>";
$name3 = $me['name']['givenName'];
echo "Nombre: $name3 <br>"; //Everything works fine until I try to get the email
$correo = ($me['emails']['value']);
echo $correo;
}
?>
</body>
</html>
我只是要从$ me获取帐户电子邮件地址以将其存储在我的应用程序中。
谢谢。