Google People API-获取列表

时间:2019-03-20 19:44:30

标签: php rest google-api-php-client google-people google-people-api

我对Google API PHP有问题。我需要在应用程序中获取用户的所有联系人。我使用官方的lib https://github.com/googleapis/google-api-php-client。我的代码的灵感来自:https://developers.google.com/people/quickstart/php

$client->setScopes(
    array(
        \Google_Service_PeopleService::CONTACTS,
        \Google_Service_PeopleService::CONTACTS_READONLY,
        \Google_Service_PeopleService::USERINFO_PROFILE,
        \Google_Service_PeopleService::USERINFO_EMAIL,
    )
);
$service_contacts = new \Google_Service_PeopleService($client);

$optParams = array(
    'pageSize' => 10,
    'personFields' => 'names,emailAddresses',
);
$results = $service_contacts->people_connections->listPeopleConnections('people/me', $optParams);
if (count($results->getConnections()) == 0) {
    echo "No connections found.<br>";
} else {
    echo "contacts :<br>";
    foreach ($results->getConnections() as $person) {
        if (count($person->getNames()) == 0) {
            echo "No names found for this connection<br>";
        } else {
            $names = $person->getNames();
            $name = $names[0];
            printf("%s<br>", $name->getDisplayName());
        }
    }
}

但结果是

Google_Service_PeopleService_ListConnectionsResponse {#712 ▼
  #collection_key: "connections"
  #connectionsType: "Google_Service_PeopleService_Person"
  #connectionsDataType: "array"
  +nextPageToken: null
  +nextSyncToken: null
  +totalItems: null
  +totalPeople: null
  #internal_gapi_mappings: []
  #modelData: array:1 [▼
    "connections" => []
  ]
  #processed: array:1 [▼
    "connections" => true
  ]
}

我也尝试过

$service_contacts = new \Google_Service_People($client);

总是没有结果,好像我没有联系人。

这对其他人有意义吗?

1 个答案:

答案 0 :(得分:0)

(代表问题作者发布)

我忘记设置用户电子邮件:

$service_contacts->getClient()->setSubject($email); //user Email