当用户通过Google Oauth2和people.me进行身份验证时,如何获取电子邮件地址 [英] How to get email address when user is authenticated with Google Oauth2 and people.me
问题描述
我刚刚接触php和google api,当用户使用Google+登录时,我正在尝试获取电子邮件地址,我使用$ plus-> people-> get(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碱基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']} \\\
< br>;
print显示名称:{$ me ['displayName']} \\\
< br>;
printImage Url:{$ me ['image'] ['url']} \\\
< br>;
printUrl:{$ me ['url']} \\\
< br>;
$ name3 = $ me ['name'] ['givenName'];
echoNombre:$ name3< br>; //一切正常,直到我尝试收到电子邮件
$ correo =($ me ['emails'] ['value']);
echo $ correo;
}
?>
< / body>
< / html>
我只需从$ me获取帐户电子邮件地址即可将其存储在我的应用程序中。 p>
谢谢。
$ me ['emails'] 是一个数组,而不是一个哈希。所以添加一个 [0] 来选择第一个Mailadress:
$ correo =($ me [ '电子邮件'] [0] [ '值']);
现在应该可以正常运行...
I'm new to php and the google api, I'm trying to get the email address when the user sing in with Google+, I get all the User info using $plus->people->get("me") but when I try to get the email address it fails with this error:
PHP Notice: Undefined index: value
here is my code:
<?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>
I just what to get the account email address from $me to store it on my app.
thanks.
$me['emails'] is an Array, not a Hash. So add an [0] for choosing the first Mailadress:
$correo = ($me['emails'][0]['value']);
Now it should work fine...
这篇关于当用户通过Google Oauth2和people.me进行身份验证时,如何获取电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!