【问题标题】:Has anyone got Google DirectoryService.Users.List().Execute(); to work in C#/.NET?有没有人有 Google DirectoryService.Users.List().Execute();在 C#/.NET 中工作?
【发布时间】:2013-07-21 19:16:53
【问题描述】:

一段时间以来,我一直在尝试列出我的 Google Apps 域中的用户。 在 Python 中没问题,但在 C# 中我收到一条错误消息: 发生错误: Google.Apis.Requests.RequestError 错误请求 [400] 错误 [ 消息[错误请求]位置[-]原因[错误请求]域[全局] ]

我不是任何类型的 C# 专家,但是当我查看 Google.Apis.Admin.directory_v1.cs - 文件时,它看起来好像 UserResource ListRequest 是错误的??? 它位于文件的第 7349-7352 行。任何人都知道它是否尚未在 API 中实现?

编辑: 我首先认为 Google.Apis.Admin.directory_v1.cs 中的代码,第 7349-7352 行是错误的(正如我所提到的 - 我不是 C#-guru):

代码:

 /// <summary>Retrieve either deleted users or all users in a domain (paginated)</summary>
    public virtual ListRequest List() {
        return new ListRequest(service);
}

为什么我觉得奇怪:

我无法看到将 customerid 或域作为参数传递给此请求的位置,但在 APIs Explorer 中需要它(否则我会收到与上述相同的错误消息,在我的原始帖子中)。

编辑:我在文件中往下看了一点,我猜第 8904 行及以后的行正在做我之前寻找的事情。我的坏!

但我仍然无法让我的代码工作?!?!?


我的代码不起作用:

static void Main(string[] args)
    {
        // Display the header and initialize the sample.
        CommandLine.EnableExceptionHandling();
        Console.WriteLine("List users in a google apps domain!");
        Console.WriteLine("by Jonas Bergstedt 2013");

        // Get the domainname
        Console.Write("Domain: ");
        string domain = Console.ReadLine();

        // Register the authenticator.
        var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
        {
            ClientIdentifier = <myClientId>,
            ClientSecret = <myClientSecret>",
        };

        var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);

        // Create the service.
        var service = new DirectoryService(new BaseClientService.Initializer()
        {
            Authenticator = auth,
            ApplicationName = "List Users",
            ApiKey = <myApiKey>
        });
        // Trying to add the domain
        service.Users.List().Domain = domain;

        Users results = service.Users.List().Execute();

        foreach (User list in results.UsersValue)
        {
            Console.WriteLine("- " + list.Name);
        }
    }
    private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
    {
        // Get the auth URL:
        IAuthorizationState state = new AuthorizationState(new[] { DirectoryService.Scopes.AdminDirectoryUser.GetStringValue() });
        state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
        Uri authUri = arg.RequestUserAuthorization(state);

        // Request authorization from the user (by opening a browser window):
        Process.Start(authUri.ToString());
        Console.WriteLine();
        Console.Write("Authorization Code: ");
        string authCode = Console.ReadLine();

        // Retrieve the access token by using the authorization code:
        return arg.ProcessUserAuthorization(authCode, state);
    }
}

【问题讨论】:

  • 您能附上您的代码吗?这些行有什么问题?为什么看起来不对?
  • @peleyal 代码已添加!有什么建议吗?

标签: c# visual-studio-2012 google-api-dotnet-client google-admin-sdk


【解决方案1】:

ListRequest 具有这些属性。看起来这些属性不是强制性的,因此它们不是构造函数的一部分。 您可以执行以下操作:

var listReq = service.Users.List();
listReq.Customer = "CUSTOMER_HERE";
listReq.Domain = "DOMAIN_HERE";
Users results = listReq.Execute();

【讨论】:

  • 谢谢,这有效! :) 但我不同意这不是强制性的。检查 APIs Explorer - 要求任一客户域。
  • 我同意它看起来不是那样的,但是在没有域或客户价值的情况下尝试一下,然后自己看看。
猜你喜欢
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 2019-04-08
相关资源
最近更新 更多