使用 Google 目录 API 找不到 OrgUnit

OrgUnit Not Found using Google Directory API

程序

I'm going to:

1. Get a OrgUnit from the Google Directory API
2. Read the OrgUnit and collect the required Data
3. Try to delete the OrgUnit I just collected.

This somehow results in a 404 [Not Found] Error
Please keep in mind that the DirectoryService Class I am using, is working properly.
I modified the code in this example to make it easy to read, for example: Exception handling is not included etc.

API

using Google.Apis.Admin.Directory.directory_v1

1.从 Google 目录中获取一个 OrgUnit API

DirectoryService directoryService = ServiceInitializers.InitializeDirectoryService();
OrgUnit oUnit = directoryService.Orgunits.List(Settings.customerId).Execute().OrganizationUnits.FirstOrDefault();


2.Read OrgUnit 并收集所需的数据

string orgUnitPath = oUnit.OrgUnitPath;


3.Try删除我刚刚收集的OrgUnit

var orgUnitDeleteResult = directoryService.Orgunits.Delete(Settings.customerId, orgUnitPath).Execute();


异常

GoogleApiException was unhandled

An unhandled exception of type 'Google.GoogleApiException' occurred in Google.Apis.dll

Additional information: Google.Apis.Requests.RequestError Org unit not found [404]

我的声誉不够高,无法在发布答案之前添加评论以获得澄清,因此我必须在这里做出一些假设。

第一个假设是您使用服务帐户访问 API。

第二个假设是您已从 Google 管理控制面板获得证书,这一切都是为了。

我在通过 API 更新用户帐户时遇到了类似的问题,为我解决的问题是让目录管理员帐户充当服务帐户的代表。

这是我用来初始化 Google 目录服务的代码。

private static DirectoryService initializeGoogleDirectoryService()
{
    try
    {
        String serviceAccountEmail = "your_service_account_email@developer.gserviceaccount.com";

        var certificate = new X509Certificate2(@"your_certificate_name.p12", "your_secret", X509KeyStorageFlags.Exportable);

        // For the service account to work, a user with admin privs must be assigned as the delegate.
        ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer(serviceAccountEmail)
           {
               // Change the scope here to the one you need to modify org units.
               Scopes = new[] { DirectoryService.Scope.AdminDirectoryUser },
               User = "administrator_account@your_google_apps_domain.com"
           }.FromCertificate(certificate));

        // Create the service.
        var service = new DirectoryService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Your_Application_Name"
        });

        return service;
    }
    catch (Exception ex)
    {
        // Exception handling code below.
        return null;
    }
    finally
    { 
    }
}