使用Google Directory API找不到OrgUnit

Nic*_*zee 9 .net c# wcf google-admin-sdk google-directory-api

程序

我要:

1.从Google Directory API获取一个OrgUnit
2.阅读OrgUnit并收集所需的数据
3.尝试删除我刚刚收集的OrgUnit.

这会以某种方式导致404 [Not Found]错误
请记住我正在使用的DirectoryService类正常工作.
我修改了这个例子中的代码,使其易于阅读,例如:不包括异常处理等.

API

using Google.Apis.Admin.Directory.directory_v1
Run Code Online (Sandbox Code Playgroud)

1.从Google Directory API获取OrgUnit

DirectoryService directoryService = ServiceInitializers.InitializeDirectoryService();
OrgUnit oUnit = directoryService.Orgunits.List(Settings.customerId).Execute().OrganizationUnits.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)


2.读取OrgUnit并收集所需的数据

string orgUnitPath = oUnit.OrgUnitPath;
Run Code Online (Sandbox Code Playgroud)


3.尝试删除我刚刚收集的OrgUnit

var orgUnitDeleteResult = directoryService.Orgunits.Delete(Settings.customerId, orgUnitPath).Execute();
Run Code Online (Sandbox Code Playgroud)


例外

GoogleApiException未处理Google.Apis.dll中

出现未处理的"Google.GoogleApiException"类型例外情况

其他信息:Google.Apis.Requests.RequestError未找到组织单位[404]

she*_*ppe 2

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

第一个假设是您正在使用服务帐户来访问 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
    { 
    }
}
Run Code Online (Sandbox Code Playgroud)