0

试图掌握如何在 C# 中使用 google-admin-sdk 的窍门(有一个可能的工作机会)

我已经设法在 Python 2.7 中创建了用于创建用户并将用户添加到组中的代码作为命令行工具。

但是雇主问我是否可以在 C# 中做同样的事情。我想我会掌握它的窍门,但会感谢一些关于如何开始的帮助。我已安装 Visual Studio Express 2012 for Desktop 并下载:google-admin-directory_v1-rev6-cshapp-1.4.0-beta.zip google-api-dotnet-client-1.4.0-beta-samples.zip google-api- dotnet-client-1.4.0-beta.zip

但我找不到任何(对我来说可以理解的)样本。

有人愿意给我任何好的指点吗?将不胜感激。:) /乔纳斯

编辑:到目前为止添加我的代码!

using System;
using System.Diagnostics;
using System.Linq;

using DotNetOpenAuth.OAuth2;

using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Samples.Helper;
using Google.Apis.Services;
using Google.Apis.Util;
using Google.Apis.Admin.directory_v1;
using Google.Apis.Admin.directory_v1.Data;



namespace Bergstedts.ListUsers
{
    public class Program    
    {
        static void Main(string[] args)
        {
             // Display the header and initialize the sample.
            CommandLine.EnableExceptionHandling();
            CommandLine.DisplayGoogleSampleHeader("Lists all Users");

            // Register the authenticator.
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
                {
                    ClientIdentifier = "my ID",
                    ClientSecret = "my secret"
                };

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

            // Create the service.
            var service = new DirectoryService(new BaseClientService.Initializer()
                {
                    Authenticator = auth,
                    ApplicationName = "List Users",

                });

            service.Users.List().Domain = "mydomain.com";
            Users results = service.Users.List().Execute();
            Console.WriteLine("Users:");
            foreach (User list in results.UsersValue)
            {
                Console.WriteLine("- " + list.Name);
            }
            Console.ReadKey();
        }

        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.Write("  Authorization Code: ");
            string authCode = Console.ReadLine();
            Console.WriteLine();

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

    }
}

编辑:找到如何添加域:

service.Users.List().Domain = "mydomain.com";

但我仍然收到相同的错误消息:发生错误:Google.Apis.Requests.RequestError Bad Request [400] Errors [ Message[Bad Request] Location[ - ] Reason[badRequest] Domain[global] ]

4

2 回答 2

1

现在已经解决了!像这样拆分 list().Execute() !从@peleyal 获得帮助

var listReq = service.Users.List();
            listReq.Domain = domain;
        Users results = listReq.Execute();
于 2013-07-23T18:25:24.427 回答
0

这是从域中获取用户的另一种方式(只是有点不同)

String serviceAccountEmail = "xxxxxxx@developer.gserviceaccount.com";
var certificate = new X509Certificate2(@"xxxxx.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { DirectoryService.Scope.AdminDirectoryUser},
User = "your USER",  
}.FromCertificate(certificate));
var service = new DirectoryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "name of your app",
});
var listReq = service.Users.List();
listReq.Domain = "your domain";
Users allUsers = listReq.Execute();
foreach(User myUser in allUsers.UsersValue){
    Console.WriteLine("*" + myUser.PrimaryEmail);
}
Console.ReadKey();

对于想要了解更多信息的人,可以访问Admin-SDK 用户:列表目录 API:限制和配额

于 2014-02-19T19:28:31.947 回答