【问题标题】:Converting Google Apps Provisioning API to Directory API for .Net code将 Google Apps Provisioning API 转换为 Directory API for .Net 代码
【发布时间】:2015-07-31 12:42:16
【问题描述】:

更新了适用于使用 Directory API 创建用户帐户的代码: 使用目录 API 将用户添加到 OU 仍然不起作用。

String serviceAccountEmail = ".........@developer.gserviceaccount.com";
X509Certificate2 certificate = new X509Certificate2(@"C:\key.p12", "secret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
    Scopes = new[]
    {
        DirectoryService.Scope.AdminDirectoryUser
    },
    User = "test@example.com",              
}.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();
}

我们使用 Google Apps Provisioning API for .Net 代码,但现在我们需要切换到目录 API,我正在按照此链接中提到的步骤操作:https://developers.google.com/admin-sdk/directory/v1/get-start/getting-started

但是我对这里提到的一些步骤感到困惑,所以如果有人能给我一些想法,我有一些问题。

以下是在 .NET 中使用 Google Apps Provisioning API 的代码,该 API 为用户创建 google 帐户,将用户添加到组织单位、组。我现在需要使用 Directory 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。但是在 Directory API 中如何包含我下载的这个库?

  1. 在 Provisioning API 中,我使用了 AppsService 并创建了这样的用户:

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

但是在 Directory API 中,我们需要使用此链接中提到的 POST 请求吗? https://developers.google.com/admin-sdk/directory/v1/guides/manage-users#create_user

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

【问题讨论】:

    标签: c# asp.net google-admin-sdk google-directory-api


    【解决方案1】:

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

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

    如果您想通过 http 请求直接调用 API,则需要指定 POST 方法。但在这种情况下,库会处理该部分,因此您可以专注于提供必要的信息。

    我知道这个example 是用于java,但由于sintaxis 类似于c#,它可以帮助您了解如何调用api 方法

    希望这会有所帮助。

    【讨论】:

    • 嗨 Gerardo,我按照您提供的示例和此处提到的示例使用 Google Directory API 创建了用户帐户:stackoverflow.com/questions/21638337/… 现在我需要将创建的用户添加到组织单位,它不起作用对我来说。
    • 您遇到了什么错误?以及如何设置用户的 OU?
    • 你好 Gerardo,我已经在我的帖子中添加了上面的用户帐户创建代码:“使用 Directory API 创建用户帐户的更新代码:”现在,我正在按照此文档添加用户到组织单位,我不知道如何执行 API。例如,为了创建用户,我这样做: User results = ser.Users.Insert(newUser).Execute();知道如何将用户添加到 OU 吗?
    • OU是用户信息中的一个属性,类似于密码。您是否尝试在用户变量中添加该参数?例如:var user = ... Password = password, orgUnitPath = 'OU' };
    猜你喜欢
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 2014-05-26
    相关资源
    最近更新 更多