Google API Client and access to Birthday and Age

随声附和 提交于 2019-12-11 15:49:05

问题


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!).

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:


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.



来源:https://stackoverflow.com/questions/49721044/google-api-client-and-access-to-birthday-and-age

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!