C#的新手。如何使用google-admin-sdk(添加用户,将用户添加到组a.s.o.)?

时间:2013-07-18 11:35:02

标签: c# google-admin-sdk

试图了解如何在C#中使用google-admin-sdk(获得可能的职位空缺)

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

但是雇主问医生我是否可以在C#中做同样的事情。我想我会掌握它,但会对如何开始有所帮助。 我已安装Visual Studio Express 2012 for Desktop并已下载:     google-admin-directory_v1-rev6-csharpp-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     不良要求[400]     错误[             消息[错误请求]位置[ - ]原因[badRequest]域[全局]     ]

2 个答案:

答案 0 :(得分:1)

现在修复了! 拆分列表()。像这样执行()!得到了@peleyal的帮助

var listReq = service.Users.List();
            listReq.Domain = domain;
        Users results = listReq.Execute();

答案 1 :(得分: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 Users: listDirectory API: Limits and Quotas