dongqie7806 2018-04-08 18:05
浏览 82

Google API客户端和访问生日和年龄

I am trying to login through Google API and getting access to some of the user data, including birthday.

This is the code and scopes I am using:

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ .'/vendor/google/apiclient/src');

require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

const CLIENT_ID = '[MY CLIENT ID]';

const CLIENT_SECRET = '[MY APP SECRET]';

const APPLICATION_NAME = "Google+ PHP Quickstart";

$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setAccessType("offline");        // offline access
$client->setIncludeGrantedScopes(true);   // incremental auth
$client->addScope('https://www.googleapis.com/auth/plus.login');
$client->addScope('https://www.googleapis.com/auth/user.birthday.read');
$client->addScope('https://www.googleapis.com/auth/userinfo.email');
$client->addScope('https://www.googleapis.com/auth/userinfo.profile');
$client->addScope('https://www.googleapis.com/auth/plus.me');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');

$auth_url = $client->createAuthUrl();

header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

When I see the login screen, I am asked to allow the app to access my birthday, as you can see below (it is in portuguese but it mentions birth date!).

enter image description here

Nonetheless, I cannot get access to the birthday nor the date.

This is what I use to get user info on the callback page:

$objOAuthService = new Google_Service_Oauth2($client);

if ($client->getAccessToken()) {
    $userData = $objOAuthService->userinfo->get();
    print_r($userData);
}

What am I missing?

I saw an answer regarding public or private visibility of the birthdate, but it seemed a strange requirement. Why does it ask the user if he allows the birthday to be shown, the user says yes, and it is not returned to the site as it is not public? I would like some official google source document stating this, instead of an answer without any official references, so I would like someone to shed some light on this with any strong information to back this up.

  • 写回答

1条回答 默认 最新

  • dtoka218420 2018-04-09 06:52
    关注

    The only way i currently know of returning the current users birthday is People.get does return birthday. However i suspect its linked to google+ so if the user hasn't filled it out you probably wont get info.

    GET https://people.googleapis.com/v1/{resourceName=people/*}

    Send Resournce name of people/me and birthdays personFields

    {
      "resourceName": "people/117200475532672775346",
      "etag": "%EgQBBzcuGgwBAgMEBQYHCAkKCwwiDDQwaGhWYzc3cXJBPQ==",
      "birthdays": [
        {
          "metadata": {
            "primary": true,
            "source": {
              "type": "PROFILE",
              "id": "117200475532672775346"
            }
          },
          "date": {
            "month": 1,
            "day": 6
          }
        },
        {
          "metadata": {
            "source": {
              "type": "ACCOUNT",
              "id": "117200475532672775346"
            }
          },
          "date": {
            "year": 1971,
            "month": 1,
            "day": 6
          }
        }
      ]
    

    Update:

    $optParams = array(
      'personFields' => 'birthdays',
    );
    $results = $service->people->get('people/me', $optParams);
    

    I dont have access to test php right now this is an educated guess.

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站