doupian9798 2015-08-25 07:49 采纳率: 100%
浏览 215
已采纳

如何使用google api正确获取用户详细信息

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.

  • 写回答

1条回答 默认 最新

  • donglun1020 2015-08-25 08:39
    关注

    i just added

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

    and now is working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?