如何使用google api正确获取用户详细信息 [英] How to get user details corectly with google api

查看:32
本文介绍了如何使用google api正确获取用户详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这段代码中有一个错误,我试图获取用户详细信息,我只需要姓名、电子邮件,但代码不返回电子邮件.我该如何解决?

setClientId($client_id);$client->setClientSecret($client_secret);$client->setRedirectUri($redirect_uri);$client->addScope("https://www.googleapis.com/auth/plus.login");$service = new Google_Service_Oauth2($client);//登出如果(isset($_REQUEST ['注销'])){未设置($_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('位置:' .filter_var($redirect, FILTER_SANITIZE_URL));}/****************************************************如果我们有访问令牌,我们可以制作请求,否则我们生成一个身份验证 URL.****************************************************/如果 (isset($_SESSION['access_token']) && $_SESSION['access_token']) {$client->setAccessToken($_SESSION['access_token']);} 别的 {$authUrl = $client->createAuthUrl();}/****************************************************如果我们已登录并要求缩短一个 URL,然后我们创建一个新的 URL 对象,设置未缩短的 URL,并调用 'insert' 方法网址"资源.请注意,我们重新存储access_token 包,以防万一在请求期间发生了变化 - 主要是可能发生在这里是访问令牌本身是如果应用程序具有离线访问权限,则刷新.****************************************************/如果 ($client->getAccessToken()) {$user = $service->userinfo->get($_POST);echo '

';打印_r($用户);echo '</pre>';$_SESSION['access_token'] = $client->getAccessToken();}echo pageHeader("用户查询 - URL 缩短器");如果(strpos($client_id,googleusercontent")==假){echo missingClientSecretsWarning();出口;}?><div class="box"><div class="请求"><?php如果(isset($authUrl)){echo "<a class='login' href='" .$authUrl ."'>连接我!</a>";} 别的 {回声<<<END<a class='logout' href='?logout'>Logout</a>结尾;}?>

代码返回这个

Google_Service_Oauth2_Userinfoplus 对象([internal_gapi_mappings:protected] =>大批([家庭名称] =>姓[给定名称] =>给定的名称[已验证电子邮件] =>已验证的电子邮件)[电子邮件] =>[家庭名称] =>NVS[性别] =>男性[给定名称] =>杰克[高清] =>[id] =>100137049524582923700[链接] =>https://plus.google.com/100137049524582923700[语言环境] =>恩[名称] =>杰克 NVS (LOGRomania)[图片] =>https://lh6.googleusercontent.com/-VdyFV_RL0SE/AAAAAAAAAAI/AAAAAAAAADo/WCrZfpemELk/photo.jpg[已验证电子邮件] =>[模型数据:受保护] =>大批([given_name] =>杰克[family_name] =>NVS)[已处理:受保护] =>大批()

在我正确获得此代码后,我希望它显示我的电子邮件和姓名,我会将它们插入到我的数据库中.但是,我怎样才能从这个 API 中获取正确的数据呢?

感谢您的帮助.

解决方案

我刚刚添加了

$client->addScope("https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email");

现在正在工作.

Hi there I have a bug in this code, I tried to get user details I need only name, email but code doesn't return the email. How can I fix it ?

<?php

include_once "templates/base.php";
session_start();

require_once ('src/Google/autoload.php');

 $client_id = '1061700181920-5i9r----something-----k3mogj328g9sed3.apps.googleusercontent.com';
 $client_secret = 'zSfeSN-----gsomething----PgtZce0uvm';
 $redirect_uri = 'http://localhost/easy_b/shop/user-example.php';

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/plus.login");


$service = new Google_Service_Oauth2($client);

//logout
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 we have an access token, we can make
  requests, else we generate an authentication URL.
 ************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $authUrl = $client->createAuthUrl();
}

/************************************************
  If we're signed in and have a request to shorten
  a URL, then we create a new URL object, set the
  unshortened URL, and call the 'insert' method on
  the 'url' resource. Note that we re-store the
  access_token bundle, just in case anything
  changed during the request - the main thing that
  might happen here is the access token itself is
  refreshed if the application has offline access.
 ************************************************/
if ($client->getAccessToken()) {
  $user = $service->userinfo->get($_POST);
echo '<pre>';
    print_r($user);
    echo '</pre>';
  $_SESSION['access_token'] = $client->getAccessToken();
}

echo pageHeader("User Query - URL Shortener");
if (strpos($client_id, "googleusercontent") == false) {
  echo missingClientSecretsWarning();
  exit;
}
?>
<div class="box">
  <div class="request">
<?php 
if (isset($authUrl)) {
  echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
} else {
  echo <<<END

    <a class='logout' href='?logout'>Logout</a>
END;
}
?>
  </div>


</div>

the code returns this

Google_Service_Oauth2_Userinfoplus Object
(
    [internal_gapi_mappings:protected] => Array
        (
            [familyName] => family_name
            [givenName] => given_name
            [verifiedEmail] => verified_email
        )

    [email] => 
    [familyName] => NVS
    [gender] => male
    [givenName] => Jack
    [hd] => 
    [id] => 100137049524582923700
    [link] => https://plus.google.com/100137049524582923700
    [locale] => en
    [name] => Jack NVS (LOGRomania)
    [picture] => https://lh6.googleusercontent.com/-VdyFV_RL0SE/AAAAAAAAAAI/AAAAAAAAADo/WCrZfpemELk/photo.jpg
    [verifiedEmail] => 
    [modelData:protected] => Array
        (
            [given_name] => Jack
            [family_name] => NVS
        )

    [processed:protected] => Array
        (
        )

After I get this code correctly I want it to show me email and name, I will insert them in my database. But, how can I get the proper data from this API?

Thanks for helping.

解决方案

i just added

$client->addScope("https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email");

and now is working.

这篇关于如何使用google api正确获取用户详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆