【问题标题】:Google API Client and access to Birthday and AgeGoogle API 客户端和访问生日和年龄
【发布时间】:2018-09-18 03:35:32
【问题描述】:

我正在尝试通过 Google API 登录并访问一些用户数据,包括生日。

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

<?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));

当我看到登录屏幕时,系统会要求我允许该应用访问我的生日,如下所示(它是葡萄牙语,但它提到了出生日期!)。

不过,我无法访问生日和日期。

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

$objOAuthService = new Google_Service_Oauth2($client);

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

我错过了什么?

我看到了关于生日的公开或私人可见性的答案,但这似乎是一个奇怪的要求。为什么它询问用户是否允许显示生日,用户说是,并且由于它不公开而没有返回到站点?我想要一些官方的谷歌源文件来说明这一点,而不是没有任何官方参考的答案,所以我希望有人能用任何强有力的信息来阐明这一点。

【问题讨论】:

  • @DaImTo 我看到了那个答案,但这似乎是一个奇怪的要求。为什么它询问用户是否允许显示生日,用户说是,并且由于它不公开而没有返回到站点?我想要一些官方的谷歌源文档来说明这一点,而不是没有任何官方参考的答案,所以我希望有人能用任何强有力的信息来阐明这一点。
  • 因为 Google+ API 非常有限,而且在 Google 启动该项目之前,没有任何错误或问题得到修复。但这只是我作为一名开发人员的看法,他花了数年时间试图让它发挥作用,但我从未从 Google 的每个人那里得到任何反馈
  • @DaImTo 还有其他选择吗? google 是否提供任何替代 Google+ API 的身份验证方式?

标签: php google-api google-signin google-login


【解决方案1】:

我目前知道返回当前用户生日的唯一方法是People.get 确实返回生日。但是我怀疑它与 google+ 相关联,所以如果用户没有填写它,您可能不会获得信息。

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

发送people/me 和生日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
      }
    }
  ]

更新:

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

我现在无法访问测试 php,这是有根据的猜测。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多