Google API: 404 Domain not found
<p>I am new to working with Google API but I have a project that requires me to access their domain to find a user's manager by email. Before I started on the code I wanted to set everything up so I followed the <a href="https://developers.google.com/sheets/quickstart/php#step_3_set_up_the_sample">example file</a> for PHP. I was able to get it to work but had some issues with refreshing the token once it expired and research pushed me towards using a <a href="https://developers.google.com/identity/protocols/application-default-credentials#callingphp">Service Account</a>, as this is a server cron script and I don't want to deal with any user interactions.</p>

<p>I created the Service Account, enabled G Suite Domain-wide Delegation, and added access for: <code>https://www.googleapis.com/auth/admin.directory.user.readonly</code></p>

<p>I get a <code>Google_Service_Exception</code> with my script. </p>

<p>The response is:</p>

<pre><code>{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Domain not found."
   }
  ],
  "code": 404,
  "message": "Domain not found."
 }
}
</code></pre>

<p>I am assuming this means it doesn't know the accounts domain but I don't see how I can resolve this. I assume that if this was a permissions issue, Google would tell me. I tried searching online but no luck as the issues I found were using a different method and the fixes weren't something that could be done on the Service Account. I am stuck right now so I hope a push in the right direction will get me on track.</p>

<p>This is the test script I am using:</p>

<pre><code>&lt;?php

require_once( __DIR__. '/vendor/autoload.php' );

define('CREDENTIALS_PATH', '/path/to/service_account.json');

define('SCOPES', implode(' ', array(
        Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY)
));

date_default_timezone_set('America/New_York');

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient() {
    $client = new Google_Client();
    $client-&gt;setApplicationName('TestingApp');
    $client-&gt;setAuthConfig(CREDENTIALS_PATH);
    $client-&gt;setScopes(SCOPES);

    return $client;
}   

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Directory($client);

// Print the first 10 users in the domain.
$optParams = array(
    'customer' =&gt; 'my_customer',
    'maxResults' =&gt; 10,
    'orderBy' =&gt; 'email',
);
$results = $service-&gt;users-&gt;listUsers($optParams);

if (count($results-&gt;getUsers()) == 0) {
    print "No users found.\n";
} else {
    print "Users:\n";
    foreach ($results-&gt;getUsers() as $user) {
        printf("%s (%s)\n", $user-&gt;getPrimaryEmail(),
            $user-&gt;getName()-&gt;getFullName());
    }
}
</code></pre>

<p>My <code>service_account.json</code> contains (cleaned obviously)</p>

<pre><code>{
    "type": "service_account",
    "project_id": "PROJECT_ID",
    "private_key_id": "PRIVATE_KEY_ID",
    "private_key": "PRIVATE_KEY",
    "client_email": "SERVICE_ACCOUNT_EMAIL.iam.gserviceaccount.com",
    "client_id": "CLIENT_ID",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/SERVICE_ACCOUNT_IDENTIFIER.iam.gserviceaccount.com"
}
</code></pre>

<p>Thanks for any assistance on this.</p>