使用谷歌的.Net目录API?

我正在尝试使用Google的.Net目录API创建我的第一个控制台应用程序。

我有一个基于Google示例的代码。 它向我展示了一些错误, 其中一个是我尝试创建服务的时候:

var service = new DirectoryService(new BaseClientService.Initializer() { Authenticator = auth, ApplicationName = "Create User", ApiKey = "" }); 

它告诉我:“错误3’Google.Apis.Services.BaseClientService.Initializer’不包含’Authenticator’的定义”

第二个错误在于此function

 private static IAuthorizationState GetAuthorization(NativeApplicationClient arg){} 

它告诉我:“DotNetOpenAuth.OAuth2.UserAgentClient”是在未引用的程序集中定义的。

在这种情况下,我键入(使用nuget控制台):PM>安装包DotNetOpenAuth -Version 4.3.4.13329 ….但它不解决我的问题。


这是我的代码:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; 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.directory_v1; using Google.Apis.Admin.Directory.directory_v1.Data; namespace GoogleDirectoryApi_test02_consola { class Program { static void Main(string[] args) { String CLIENT_ID = "YOUR_CLIENT_ID"; String CLIENT_SECRET = "YOUR_CLIENT_SECRET"; // Register the authenticator and create the service var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET); var auth = new OAuth2Authenticator(provider, GetAuthorization); //New User User newuserbody = new User(); string userId = "SampleId01"; UserName newusername = new UserName(); newuserbody.PrimaryEmail = userId; // Create the service. var service = new DirectoryService(new BaseClientService.Initializer() { Authenticator = auth, ApplicationName = "Create User", ApiKey = "" }); User results = service.Users.Insert(newuserbody).Execute(); } private static IAuthorizationState GetAuthorization(NativeApplicationClient arg) { // Get the auth URL: //IAuthorizationState state = new AuthorizationState(new[] { DirectoryService.Scopes.AdminDirectoryUser.GetStringValue() }); IAuthorizationState state = new AuthorizationState(new[] { DirectoryService.Scope.AdminDirectoryUser.ToString() }); 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); } } } 

在此先感谢您的帮助

必须拥有一个服务帐户,其中包含执行Google Apps域范围的权限授权

https://developers.google.com/admin-sdk/directory/v1/guides/delegation

它说要在“管理第三方OAuth客户端访问”中添加它,我有“管理OAuth客户端访问”

  String serviceAccountEmail = "......@developer.gserviceaccount.com"; X509Certificate2 certificate = new X509Certificate2(@"C:\key.p12", "notasecret", X509KeyStorageFlags.Exportable); ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail) { Scopes = new[] { DirectoryService.Scope.AdminDirectoryUser }, User = "admin@domain.com" }.FromCertificate(certificate)); var ser = new DirectoryService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Get it to work", }); User newuserbody = new User(); UserName newusername = new UserName(); newuserbody.PrimaryEmail = "jack@domain.com"; newusername.GivenName = "jack"; newusername.FamilyName = "black"; newuserbody.Name = newusername; newuserbody.Password = "password"; User results = ser.Users.Insert(newuserbody).Execute();