更新後的代碼可用於使用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");
這裏是我的問題:
: 通過此鏈接安裝客戶端庫我下載了.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請求,如果任何人都可以給我示例或想法,那就太棒了。
嗨Gerardo,我使用Google Directory API創建了用戶帳戶,下面是您提供的示例以及此處提到的示例:http:// stackoverflow。com/questions/21638337/using-googles-directory-api-for-net 現在我需要將創建的用戶添加到組織單元,它不適合我。 – TechPro
你得到了什麼錯誤?你如何設置用戶的OU? – Gerardo
Hello Gerardo,我在上面的帖子中添加了用戶帳戶創建代碼:「使用Directory API創建用戶帳戶的更新代碼:」 現在,我正在按照此文檔將用戶添加到組織單元,I不知道我可以如何執行API。例如,創建用戶我這樣做: 用戶結果= ser.Users.Insert(newUser).Execute(); 任何想法如何將用戶添加到OU? – TechPro