I am starting to use Google's Gmail API and have run into an issue which I'm finding difficult to figure out, with the eventual idea being to send emails through Google's IMAP service. This is being generated by a webserver using PHP and a Gmail/Gsuite service account which has been set to full access.
<?php
require_once 'googleapi/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=googleapi/access.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/gmail.labels');
$service = new Google_Service_Gmail($client);
// Print the labels in the user's account.
$user = 'admin@xxxx.com';
$results = $service->users_labels->listUsersLabels($user);
if (count($results->getLabels()) == 0) {
print "No labels found.\n";
} else {
print "Labels:\n";
foreach ($results->getLabels() as $label) {
printf("- %s\n", $label->getName());
}
}
?>
The unhelpful error message being returned is:
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 400, "message": "Precondition check failed.", "errors": [ { "message": "Precondition check failed.", "domain": "global", "reason": "failedPrecondition" } ], "status": "FAILED_PRECONDITION" } } ' in /home/thishost/public_html/domain.com/admin/googleapi/src/Google/Http/REST.php:123 Stack trace: #0 /home/thishost/public_html/domain.com/admin/googleapi/src/Google/Http/REST.php(98): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /home/thishost/public_html/domain.com/admin/googleapi/src/Google/Task/Runner.php(176): call_user_func_array(Array, Array) #3 /home/thishost/public_html/domain.com/admin/googleapi/src/Google/Http/REST.php(61): Google_Task_Runner->run() #4 /home/thishost/public_htm in /home/thishost/public_html/domain.com/admin/googleapi/src/Google/Http/REST.php on line 123