PHP Google People API - 简单的电子邮件请求失败

时间:2016-07-10 03:00:04

标签: php api oauth

感谢大家的时间。

我花了一整天的时间在这上面,虽然无法找到一个好的例子和答案,所以我认为这可能与社区有关。

<?php
session_start();
require_once ('Google/src/Google/autoload.php');

$client = new Google_Client();
$client->setApplicationName("xxxxx");
$client->setClientId('xxxxx');
$client->setClientSecret('xxxxx')
$client->setRedirectUri('xxxxx');
$client->addScope('profile');
$client->addScope('email');
$client->addScope('https://www.googleapis.com/auth/contacts.readonly');

if (isset($_GET['oauth'])) {
    // Start auth flow by redirecting to Google's auth server
    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else if (isset($_GET['code'])) {
    // Receive auth code from Google, exchange it for an access token, and redirect to your base URL
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
} else if ($_SESSION['access_token']) {
    // You have an access token; use it to call the People API
    $client->setAccessToken($_SESSION['access_token']);
    $service = new Google_Service_People($client);
    $optParams = array('pageSize' => 10);

    // TODO: Use service object to request People data
    $results = $service->people_connections->listPeopleConnections('people/me', $optParams);

} else {
    $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/?oauth';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

执行脚本后,我只是得到一个空白页面,如果我注释掉$ results = $ service-&gt; people_connections-&gt; listPeopleConnections(&#39; people / me&#39;,$ optParams)脚本然后的工作原理。

看起来它正确加载API并使用Oauth进行身份验证并设置了令牌会话变量;虽然取消注释它只是一个白色的屏幕,所以我认为问题是从Google People API中检索数据。

我试图下载每个用户联系人列表(包括姓名和电子邮件)以通过电子邮件分享链接;点击&#39;使用Google登录&#39;按钮。

非常感谢任何帮助,谢谢您的时间!

0 个答案:

没有答案