如何在C#上使用G Suite Admin SDK从组织(orgUnit)获取所有用户?

时间:2017-11-05 18:00:23

标签: c# google-admin-sdk

如何在C#上使用G Suite Admin SDK从组织(orgUnit)获取所有用户?

我的代码:

var request = _service.Users.List();
request.Customer = Customer;
request.Query = $"orgUnitPath={orgUnitPath}"; //orgUnitPath = "/01 Institute"
var result = request.Execute(); //get INVALID_OU_ID error

2 个答案:

答案 0 :(得分:1)

您是否尝试在组织单位路径上放置单引号?

类似的东西:

var request = _service.Users.List();
request.Customer = Customer;
request.Query = $"orgUnitPath='{orgUnitPath}'"; //orgUnitPath = '/01 Institute'
var result = request.Execute(); //get INVALID_OU_ID error

这是Google没有记录的内容,但它应该有效。

答案 1 :(得分:0)

您可以尝试文档中提供的this code

using Google.Apis.Auth.OAuth2;
using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Admin.Directory.directory_v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DirectoryQuickstart
{
    class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/admin-directory_v1-dotnet-quickstart.json
        static string[] Scopes = { DirectoryService.Scope.AdminDirectoryUserReadonly };
        static string ApplicationName = "Directory API .NET Quickstart";

        static void Main(string[] args)
        {
            UserCredential credential;

            using (var stream =
                new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/admin-directory_v1-dotnet-quickstart.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Directory API service.
            var service = new DirectoryService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });

            // Define parameters of request.
            UsersResource.ListRequest request = service.Users.List();
            request.Customer = "my_customer";
            request.MaxResults = 10;
            request.OrderBy = UsersResource.ListRequest.OrderByEnum.Email;

            // List users.
            IList<User> users = request.Execute().UsersValue;
            Console.WriteLine("Users:");
            if (users != null && users.Count > 0)
            {
                foreach (var userItem in users)
                {
                  Console.WriteLine("{0} ({1})", userItem.PrimaryEmail,
                      userItem.Name.FullName);
                }
            }
            else
            {
                Console.WriteLine("No users found.");
            }
            Console.Read();

        }
    }
}

如需进一步参考,请参阅documentation

SO post也可以找到其他信息。