Error "(get) unknown parameter: 'personFields'" when following examples in Google People API documentation
Asked Answered
K

3

6

I am trying to use the Google People API to add and update my Google contacts. I have set up the API almost verbatim with the example code given in Google's documentation (https://developers.google.com/people/v1/getting-started). I get an error for the following line of code, which again comes verbatim from the documentation:

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

The error is as follows:

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

I have seen an answer to a similar question (Can't access my profile data when accessing google-people API) suggest using the Google_Service_PeopleService class instead of the Google_Service_People class as called for in the documentation. However, when I try to do this, I get another error:

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

At this point, I am at a total loss. I would like to use Google Contacts to keep track of my contacts and would like to be able to work with Google Contacts via PHP so that I can add them to my database. Can anyone shed any light on this? Thank you in advance for any help!

UPDATE: I tried moving the page to a local server on my computer (forgetting that Google redirects the page to my website) to see if I could get a different result from a different server. Somehow, magically, it worked when I tried to access the script from my website—for about 15-30 minutes or so. Then, I got the same error as before. If I run the page from my localhost server, I will get caught in an endless authorization redirect loop (to be expected given that Google redirects me to the online page), but then the online page will work for a time.

At this point, I can't begin to guess where the issue is. Does this make sense to anyone else?

Kowalewski answered 25/8, 2017 at 5:3 Comment(6)
Are you positive that you have the latest version of the Client Library?Tautomerism
I have the same problem, but using the Google_Service_PeopleService fixed it for me; I suggest you focus on resolving the second error.Splinter
I am using version 2.2.0 of the Google API PHP Client library, which I believe to be the correct version (though I could always be mistaken). I am somewhat at a loss about the authentication credentials error when using Google_Service_PeopleService. I have OAuth 2 credentials, which work just fine when using the Google_Service_People class. It's only when I use the Google_Service_PeopleService class that it throws an error. Is there something in my credentials I should look at or change?Kowalewski
@DanielBaker you correctly set $people_service = new Google_Service_PeopleService($client);? Perhaps you need to reauthenticate yourself as user?Splinter
@Splinter Thanks for the help! How can I reauthenticate myself as a user? I cleared my browsing data and then reloaded the page, but got the same error. Is this something I can do through the developer console?Kowalewski
@DanielBaker perhaps revoking your user consent / API access. And granting it again or disabling & re-enabling the API in the dashboard has any effect?Splinter
E
4

This worked fine for me:

$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>';
  }
}
Engdahl answered 17/12, 2017 at 4:22 Comment(1)
tried the above method, but in my case i get $connections empty array. anything i may be missing? cause the same account used to return connections earlier.Sifuentes
D
3

I guess I know whats happens. My English not very well so I hope you can understand what I mean..

In my case, I'm using Google_Service_People class (src\Google\Service\People.php) and get same error as you. So I take a look about People.php and see this :

$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',
            ),
          ),
        ),

I check the official document and it say "requestMask.includeField is already DEPRECATED." but it still in api's code by somehow.

So I change it to this :

$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',
            ),
          ),

After that every thing go well.

If you are using Google API Client Libraries from PHP. This client library is in beta. So there something in their api doesn't look like how it exactly is in their api document.

So that is my case. English not my language so I hope you can understand what I trying to describe.

Delfeena answered 1/12, 2017 at 8:29 Comment(1)
my code was working till 24th August, suddenly it stopped working and error(exception) i get is for requestMask.includeField. tried to add personFileds to optParams but it gives errorGoogle_Exception: (list) unknown parameter: 'personFields' . what to do now? any suggestion?Sifuentes
K
1

John Rodriguez solved the issue. Thank you! My library is a little bit different, but the problem was the same. In my case I am using the Google_Service_PeopleService class instead of the Google_Service_People class. The path to the file I changed is: google-api-php-client-2.2.0/vendor/google/apiclient-services/src/Google/Service/PeopleService.php. In this case, $this->people is a Google_Service_PeopleService_Resource_People class which contains an array of methods. I commented out the depreciated code. Here is the relevant code:

$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(
           ...
            ),
          ),
        ),

So far this has worked as it should, but I have not thoroughly tested it yet. Thank you for your help!

Kowalewski answered 3/2, 2018 at 22:55 Comment(1)
Glad to help. How about choice me as accepted answer ? ;-)Delfeena

© 2022 - 2024 — McMap. All rights reserved.