2015-05-19 83 views
0

更新后的代码可用于使用Directory API创建用户帐户: 使用目录API将用户添加到OU仍然不起作用。将Google Apps Provisioning API转换为.Net代码的Directory API

String serviceAccountEmail = "[email protected]"; 
X509Certificate2 certificate = new X509Certificate2(@"C:\key.p12", "secret", X509KeyStorageFlags.Exportable); 

ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail) 
{ 
    Scopes = new[] 
    { 
     DirectoryService.Scope.AdminDirectoryUser 
    }, 
    User = "[email protected]",    
}.FromCertificate(certificate)); 

var ser = new DirectoryService(new BaseClientService.Initializer() 
{ 
    HttpClientInitializer = credential, 
    ApplicationName = "Google Account", 
}); 

try 
{       
    var user = new Google.Apis.Admin.Directory.directory_v1.Data.User() 
    { 
     Name = new Google.Apis.Admin.Directory.directory_v1.Data.UserName() 
     { 
      GivenName = FirstName.Text, 
      FamilyName = LastName.Text 
     }, 
     Password = password 
    }; 

    User newUser = new User(); 
    UserName newUserName = new UserName(); 
    newUser.PrimaryEmail = Email.Text; 
    newUserName.GivenName = FirstName_txt.Text; 
    newUserName.FamilyName = LastName_txt.Text; 
    newUser.Name = newUserName; 
    newUser.Password = password; 

    User results = ser.Users.Insert(newUser).Execute(); 
} 

我们使用的.Net代码谷歌企业应用套件配置API,但现在我们需要切换到目录API,我下面这个链接中提到的步骤:https://developers.google.com/admin-sdk/directory/v1/get-start/getting-started

但我感到困惑的一些在这里提到的步骤中,我有一些问题,如果有人可以给我一些想法。

下面是在.NET中使用Google Apps Provisioning API的代码,该API为用户创建Google帐户,将用户添加到组织单位,组。我现在需要使用目录API来实现同样的目标:

using System.Collections.ObjectModel; 
using System.Management.Automation; 
using System.Management.Automation.Runspaces; 
using Google.GData.Apps; 
using System.Text.RegularExpressions; 

//Creating Google Account 

AppsService appService = new AppsService("Mydomain", "AdminUsername", "AdminPassword"); 
try 
{ 
    //Create Google Account with the information we got through database 
    var account = appService.CreateUser(googleEmail, FirstName.Text, LastName.Text, password); 
    ResultsLabel.Text += "<br />Google Account created"; 
} 
catch (Exception ex) 
{ 
    ResultsLabel.Text += "<br />Can't add this user to Google Account"; 
}         

//Adding User to Organization Unit 

OrganizationService service = new OrganizationService("mydomain", "applicationName"); 
try 
{ 
    service.setUserCredentials("AdminUsername", "AdminPassword"); 
    service.UpdateOrganizationUser("GoogleCustomerId", Email.Text, "Staff", "/"); 
    ResultsLabel.Text += "<br />Added to Organization Unit"; 
} 
catch (Exception ex) 
{ 
    ResultsLabel.Text += "<br />Can't add to Organization Unit"; 
} 

//Adding User to the group 

appService.Groups.AddMemberToGroup(Email.Text, "Staff"); 

这里是我的问题:

  1. 在它被提到的步骤:https://developers.google.com/adminsdk/directory/v1/libraries

: 通过此链接安装客户端库我下载了.NET客户端库,但是如何将其包含在我的代码中,我不确定。在Google Apps Provisioning API中,我用作参考,并使用名称空间,例如:使用Google.GData.Apps。但在目录API中如何包含我已下载的这个库?

  • 在配置API我已经使用AppsService和创建的用户是这样的:

    var account = appService.CreateUser(googleEmail, FirstName.Text, LastName.Text, password);

  • 但在指南API我们需要使用POST请求作为在此链接中提到? https://developers.google.com/admin-sdk/directory/v1/guides/manage-users#create_user

    如何在我上面的现有代码中执行POST请求,如果任何人都可以给我示例或想法,那就太棒了。

    回答

    1

    for#1您可以使用NuGet下载library,正如您所提到的,您可以在项目中引用它。

    #2创建用户时,该方法将类似于您之前使用的方法,您将不得不提供用户信息。该方法将自动创建POST调用。

    如果您想通过http请求直接调用API,则需要指定POST方法。但在这种情况下,图书馆会照顾那部分,以便您可以专注于提供必要的信息。

    我知道这是example为Java,但作为sintaxis类似于C#中,它可以帮助你了解如何调用API方法

    希望这有助于。

    +0

    嗨Gerardo,我使用Google Directory API创建了用户帐户,下面是您提供的示例以及此处提到的示例:http:// stackoverflow。com/questions/21638337/using-googles-directory-api-for-net 现在我需要将创建的用户添加到组织单元,它不适合我。 – TechPro

    +0

    你得到了什么错误?你如何设置用户的OU? – Gerardo

    +0

    Hello Gerardo,我在上面的帖子中添加了用户帐户创建代码:“使用Directory API创建用户帐户的更新代码:” 现在,我正在按照此文档将用户添加到组织单元,I不知道我可以如何执行API。例如,创建用户我这样做: 用户结果= ser.Users.Insert(newUser).Execute(); 任何想法如何将用户添加到OU? – TechPro

    相关问题