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?
$people_service = new Google_Service_PeopleService($client);
? Perhaps you need to reauthenticate yourself as user? – Splinter