【发布时间】: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");
这是我的问题:
- 在步骤中提到: 使用此链接安装客户端库:https://developers.google.com/adminsdk/directory/v1/libraries
我下载了 .NET 客户端库,但我不确定如何将它包含在我的代码中。在 Google Apps Provisioning API 中,我用作参考并使用了命名空间,例如:使用 Google.GData.Apps。但是在 Directory API 中如何包含我下载的这个库?
-
在 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