2017-08-25 82 views
2

我试图使用Google People API添加和更新我的Google联系人。我使用Google文档(https://developers.google.com/people/v1/getting-started)中给出的示例代码几乎逐字地设置了API。我得到一个错误以下行的代码,它又来了逐字从文档:错误“(获取)未知参数:'personFields'”在Google People API文档中的示例中使用

$profile = $people_service->people->get('people/me', array('personFields' => 'names,emailAddresses')); 

错误如下:

Fatal error: Uncaught exception 'Google_Exception' with message '(get) unknown parameter: 'personFields'' in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Service/Resource.php:147 Stack trace: #0 /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/vendor/google/apiclient-services/src/Google/Service/People/Resource/People.php(52): Google_Service_Resource->call('get', Array, 'Google_Service_...') #1 /home/danbak15/bakerlegalservicesmo.com/office/BLScontacts.php(36): Google_Service_People_Resource_People->get('people/me', Array) #2 {main} thrown in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Service/Resource.php on line 147 

我所看到的是回答了类似的问题(Can't access my profile data when accessing google-people API )建议使用Google_Service_PeopleService类而不是文档中所要求的Google_Service_People类。然而,当我尝试这样做,我得到另一个错误:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 401, "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "domain": "global", "reason": "unauthorized" } ], "status": "UNAUTHENTICATED" } } ' in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Http/REST.php:118 Stack trace: #0 /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'G in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Http/REST.php on line 118 

在这一点上,我在全盘损失。我想使用Google通讯录来跟踪我的联系人,并希望能够通过PHP使用Google通讯录,以便将它们添加到我的数据库中。任何人都可以对此有所了解吗?预先感谢您的任何帮助!

更新: 我试着将页面移动到我的计算机上的本地服务器(忘记Google将页面重定向到我的网站),以查看是否可以从不同的服务器获得不同的结果。不知何故,当我尝试从我的网站上访问脚本时,它有效地工作了大约15-30分钟左右。然后,我得到了和以前一样的错误。如果我从本地主机服务器运行该页面,我将陷入无尽的授权重定向循环(预计Google会将我重定向到在线页面),但这个在线页面一段时间就会工作。

在这一点上,我无法开始猜测问题出在哪里。这对其他人有意义吗?

+0

您确定您拥有最新版本的客户端库吗? – paolo

+0

我有同样的问题,但使用Google_Service_PeopleService修复了它;我建议你专注于解决第二个错误。 – Isaiah

+0

我使用的是Google API PHP Client 2.2.0版本,我认为它是正确的版本(尽管我总是可能会误)。在使用Google_Service_PeopleService时,我对于身份验证凭证错误感到有些不知所措。我有OAuth 2凭据,在使用Google_Service_People类时工作得很好。只有当我使用Google_Service_PeopleService类时才会引发错误。我应该查看或更改我的凭据中是否有某些内容? –

回答

0

我想我知道最新情况。 我的英语不是很好,所以我希望你能明白我的意思。

在我的情况,我使用Google_Service_People类(src \ Google \ Service \ People.php)并得到与你一样的错误。所以我来看看有关People.php,看看这个:

$this->people = new Google_Service_People_Resource_People(
    $this, 
    $this->serviceName, 
    'people', 
    array(
     'methods' => array(
     'get' => array(
      'path' => 'v1/{+resourceName}', 
      'httpMethod' => 'GET', 
      'parameters' => array(
      'resourceName' => array(
       'location' => 'path', 
       'type' => 'string', 
       'required' => true, 
      ), 
      'requestMask.includeField' => array( // <= This is the problem. 
       'location' => 'query', 
       'type' => 'string', 
      ), 
     ), 
     ), 

我检查的官方文件,它说:“requestMask.includeField已经过时了。”但它仍然在api代码以某种方式。

所以我把它改成这样:

$this->people = new Google_Service_People_Resource_People(
    $this, 
    $this->serviceName, 
    'people', 
    array(
     'methods' => array(
     'get' => array(
      'path' => 'v1/{+resourceName}', 
      'httpMethod' => 'GET', 
      'parameters' => array(
      'resourceName' => array(
       'location' => 'path', 
       'type' => 'string', 
       'required' => true, 
      ), 
      'personFields' => array( // <- Change to 'personFields' 
       'location' => 'query', 
       'type' => 'string', 
      ), 
     ), 

之后每一件事情顺利。

如果您使用来自PHP的Google API客户端库。此客户端库位于测试版中。所以他们的api中有一些东西看起来并不像api文档中的确切内容。

这就是我的情况。英语不是我的语言,所以我希望你能理解我想描述的内容。

0

这个工作对我罚款:

$scopes[] = 'https://www.googleapis.com/auth/contacts.readonly'; 
$client->setScopes($scopes); 
$service = new Google_Service_People($client); 
$optParams = ['requestMask.includeField' => 'person.names']; 
$connections = $service->people_connections->listPeopleConnections('people/me', $optParams)->getConnections(); 
foreach ($connections as $connection) { 
    if (!empty($connection->getNames()[0])) { 
    print $connection->getNames()[0]->getDisplayName() . "\n" . '<br>'; 
    } 
} 
0

约翰·罗德里格斯解决的问题。谢谢!我的图书馆有点不同,但问题是一样的。在我的情况下,我使用Google_Service_PeopleService类而不是Google_Service_People类。我改变的文件路径是:google-api-php-client-2.2.0/vendor/google/apiclient-services/src/Google/Service/PeopleService.php。在这种情况下,$ this-> people是一个包含一组方法的Google_Service_PeopleService_Resource_People类。我评论了折旧的代码。这里是相关的代码:

$this->people = new Google_Service_PeopleService_Resource_People(
    $this, 
    $this->serviceName, 
    'people', 
    array(
     'methods' => array(
     'createContact' => array(
       ... 
      ), 
     ), 
     ),'deleteContact' => array(
      ... 
      ), 
     ), 
     ),'get' => array(
      'path' => 'v1/{+resourceName}', 
      'httpMethod' => 'GET', 
      'parameters' => array(
      'resourceName' => array(
       'location' => 'path', 
       'type' => 'string', 
       'required' => true, 
      ), 
      'personFields' => array(
       'location' => 'query', 
       'type' => 'string', 
      ), 
/*   'requestMask.includeField' => array(
       'location' => 'query', 
       'type' => 'string', 
      ), */ 
     ), 
     ),'getBatchGet' => array(
      'path' => 'v1/people:batchGet', 
      'httpMethod' => 'GET', 
      'parameters' => array(
      'personFields' => array(
       'location' => 'query', 
       'type' => 'string', 
      ), 
/*   'requestMask.includeField' => array(
       'location' => 'query', 
       'type' => 'string', 
      ), */ 
      'resourceNames' => array(
       'location' => 'query', 
       'type' => 'string', 
       'repeated' => true, 
      ), 
     ), 
     ),'updateContact' => array(
      ... 
      ), 
     ), 
     ), 

到目前为止这已经工作,因为它应该,但我还没有彻底测试它呢。感谢您的帮助!

相关问题