Google API客户端并访问生日和年龄 [英] Google API Client and access to Birthday and Age

查看:232
本文介绍了Google API客户端并访问生日和年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过Google API登录并获取一些用户数据,包括生日。



这是我使用的代码和范围:

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

require_once __DIR __。'/ vendor / autoload.php';

使用Symfony \Component\HttpFoundation\Request;
使用Symfony \Component\HttpFoundation\Response;

const CLIENT_ID ='[我的客户ID]';

const CLIENT_SECRET ='[我的APP秘诀]';

常规APPLICATION_NAME =Google+ PHP快速入门;

$ client = new Google_Client();
$ client-> setClientId(CLIENT_ID);
$ client-> setClientSecret(CLIENT_SECRET);
$ client-> setAccessType(offline); //离线访问
$ client-> setIncludeGrantedScopes(true); //增量认证
$ 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));

当我看到登录屏幕时,我被要求让应用程序访问我的生日,可以在下面看到(这是葡萄牙语,但它提到了出生日期!)。



尽管如此,我无法获取生日和日期。



这是我在回调页面上获取用户信息的方法:

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

我错过了什么?



我看到关于出生日期的公开或私人知名度的答案,但这似乎是一个奇怪的要求。为什么它询问用户他是否允许显示生日,用户说是,并且它不返回到该网站,因为它不公开?我希望一些官方谷歌源文件说明这一点,而不是没有任何官方引用的答案,所以我希望有人通过任何强大的信息来反映这一点。

/ people / getrel =nofollow noreferrer> People.get 确实会返回生日。然而我怀疑它的链接到谷歌+,所以如果用户没有填写它,你可能不会得到信息。



lockquote

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


发送 people / me 和生日 personFields

$ {
resourceName:people / 117200475532672775346,
etag:
$ b

  %EgQBBzcuGgwBAgMEBQYHCAkKCwwiDDQwaGhWYzc3cXJBPQ ==,
birthdays:[
{
metadata:{
primary:true,
source:{
type:PROFILE,
id:117200475532672775346
}
},
date:{
month: 1,
日:6
}
},
{
元数据:{
源:{
键入:ACCOUNT,
id:11720047553267 2775346

},
date:{
year:1971,
month:1,
day: 6
}
}
]

更新:

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

我目前无法访问测试PHP,这是一个受过教育的猜测。


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.

解决方案

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.

这篇关于Google API客户端并访问生日和年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆