Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 960
Daniel Baker Avatar asked Aug 25 '17 05:08

Daniel Baker


2 Answers

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>';
  }
}
like image 97
Eduardo Telaya Avatar answered Nov 15 '22 00:11

Eduardo Telaya


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.

like image 37
Chen Kuan Hung Avatar answered Nov 14 '22 22:11

Chen Kuan Hung