1

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.

Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56
  • Possible duplicate of [How to specify the scope of Google API to get the birthday](https://stackoverflow.com/questions/23896424/how-to-specify-the-scope-of-google-api-to-get-the-birthday) – Linda Lawton - DaImTo Apr 08 '18 at 18:10
  • @DaImTo I saw that answer, 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. – Miguel Mesquita Alfaiate Apr 08 '18 at 18:45
  • Because the Google+ API is extremely limited and none of the bugs or issues were ever fixed before Google have up on the project. But thats just my opinion as a developer who spent years trying to get it to work and never got any feedback from everyone at Google that I asked – Linda Lawton - DaImTo Apr 08 '18 at 19:06
  • @DaImTo and is there any other option? does google provide any alternative to Google+ API for authentication? – Miguel Mesquita Alfaiate Apr 09 '18 at 06:48

1 Answers1

2

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Can you provide me with an example on how to call that using the google sdk? Can't find reference on that. Well I found this https://developers.google.com/people/v1/getting-started but I get a not found error on $people_service = new Google_Service_PeopleService($client); – Miguel Mesquita Alfaiate Apr 09 '18 at 08:30
  • they have a sample here https://developers.google.com/people/quickstart/php check update – Linda Lawton - DaImTo Apr 09 '18 at 08:36