2015-05-19 82 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

    相關問題