Google API: 404 域未找到

Google API: 404 Domain not found

我是 Google API 的新手,但我有一个项目要求我访问他们的域以通过电子邮件查找用户的经理。在开始编写代码之前,我想设置所有内容,所以我遵循 example file 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 Service Account,因为这是一个服务器 cron 脚本,我不想处理任何用户交互。

我创建了服务帐户,启用了 G Suite 全域委派,并添加了对以下对象的访问权限:https://www.googleapis.com/auth/admin.directory.user.readonly

我的脚本得到了 Google_Service_Exception

响应是:

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

我假设这意味着它不知道帐户域,但我不知道如何解决这个问题。我假设如果这是一个权限问题,Google 会告诉我。我尝试在线搜索但没有成功,因为我发现的问题是使用不同的方法,并且修复不是可以在服务帐户上完成的。我现在被困住了,所以我希望朝着正确的方向前进会让我走上正轨。

这是我使用的测试脚本:

<?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->setApplicationName('TestingApp');
    $client->setAuthConfig(CREDENTIALS_PATH);
    $client->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' => 'my_customer',
    'maxResults' => 10,
    'orderBy' => 'email',
);
$results = $service->users->listUsers($optParams);

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

我的service_account.json包含(明显清理)

{
    "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"
}

感谢您对此提供的任何帮助。

好的,这是一个非常容易忽略的步骤,但它是一个非常简单的修复程序。

这里的问题是帐户的域未被识别。我的印象是服务帐户已经附加到域,但事实并非如此。因此,修复只是添加到客户端以将其设置为域中的用户的一行代码(对于我的情况)。

我的解决方法是添加:

$client->setSubject('account@domain.com');

我的 getClient 方法。

所以现在方法看起来像:

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient() {
    $client = new Google_Client();
    $client->setApplicationName('TestingApp');
    $client->setAuthConfig(CREDENTIALS_PATH);
    $client->setScopes(SCOPES);
    $client->setSubject('account@domain.com');
    return $client;
}

我看到这个提到 in the API 但它声明它是可选的。希望这也会对其他人有所帮助。

对我来说是同样的错误,但我需要与服务帐户电子邮件(在 json 授权文件中找到)共享我的日历。之后,错误消失了。

就我而言,是我通过 listUsers() 函数传递的域值导致了此错误。假设我的 GSuite 域名是 xyz.com,我尝试过这样的域名

$dir = new \Google_Service_Directory($googleClient);
$dir->users->listUsers(array('domain' => 'abc.com', 'maxResults' => 500));

相反,我应该使用正确的域名作为 'domain' 的值,如下所示。

$dir = new \Google_Service_Directory($googleClient);
$dir->users->listUsers(array('domain' => 'xyz.com', 'maxResults' => 500));