Search code examples
phpgoogle-api-php-clientgoogle-workspace

Does fixing a "domain not found" error require a G-Suite account


In a nutshell:

is it possible to have a server side script which inserts members in a Google group even if I don't have a G-Suite account? And if yes, what does a Domain not found refers to?

In more details

I'm trying to help a small NGO to automate part of their work managing the registrations of new users. In particular I'm setting up a script which will insert new members in a google group.

Following the doc I:

the code is:

<?php
require __DIR__ . '/vendor/autoload.php';

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setApplicationName("GoogleGroup MA AutoUpdate");
$client->setScopes(array(
      Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY,
      Google_Service_Directory::ADMIN_DIRECTORY_GROUP
));

$user_to_impersonate = '[email protected]';
$client->setSubject($user_to_impersonate);
echo "Built client\n";

$service = new Google_Service_Directory($client);
$member = new Google_Service_Directory_Member();
$member->setEmail("[email protected]");
$member->setRole("MEMBER");
$groupEmail="[email protected]";
echo "going to insert member\n";
$service->members->insert( $groupEmail, $member);
echo "DONE\n";

but when this script runs it fails with this output

Built client
going to insert member
PHP Fatal error:  Uncaught Google_Service_Exception: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Domain not found."
   }
  ],
  "code": 404,
  "message": "Domain not found."
 }
}
 in /some/path/vendor/google/apiclient/src/Google/Http/REST.php:118
Stack trace:
#0 /some/path/vendor/google/apiclient/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#1 /some/path/vendor/google/apiclient/src/Google/Task/Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#2 /some/path/vendor/google/apiclient/src/Google/Http/REST.php(58): Google_Task_Runner->run()
#3 /some/path/vendor/google/apiclient/src/Google/Client.php(808): Google_Http_REST::execute(Object(GuzzleHttp\Client), Obje in /documents/zwp/helloasso2googlegroups/vendor/google/apiclient/src/Google/Http/REST.php on line 118

And I don't understand what this error means, and all the doc I find gives me the feeling that it's related to a notion of domain that I should link to a G Suite account, but I'm not really sure.

Long story short: I would be thrilled if you can either give me pointers to fix this error, or confirm that I can't do it without a G-Suite account.


Solution

  • The Directory API only supports G Suite, Education and Government editions Google accounts.

    Anyway, the error 404 is not related to your account status, but to a missing or wrong domain in your credentials. If this was your case you would get a 403 error "Not Authorized to access this resource/api"

    For more info about the Directory API Prerequisties: https://developers.google.com/admin-sdk/directory/v1/guides/prerequisites