2013-07-03 73 views
1

我正嘗試使用Google Directory API庫for .NET來維護域的電子郵件地址。最新的圖書館是google-admin-directory_v1-rev6-csharp-1.4.0-beta。到目前爲止,我收到的最好和最遠的信息是收到403錯誤(未授權訪問此資源/ api)。你使用過Google的Directory API嗎?

有沒有人在那裏成功地使用過它?如果是這樣,你可以分享一些代碼,技巧或竅門嗎?

回答

3

我已經使用它並在創建控制檯應用程序方面取得了一些成功。 目前我正試圖找到一種方法來跳過授權碼的複製/粘貼。 不要忘記在API控制檯中啓用API訪問。 我會告訴你一個小樣本:

using System; 
using System.Diagnostics; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Security.Cryptography; 
using System.Security.Cryptography.X509Certificates; 

using DotNetOpenAuth.OAuth2; 

using Google.Apis.Authentication.OAuth2; 
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth; 
using Google.Apis.Samples.Helper; 
using Google.Apis.Services; 
using Google.Apis.Util; 
using Google.Apis.Admin.directory_v1; 
using Google.Apis.Admin.directory_v1.Data; 



namespace Bergstedts.CreateNewUser 
{ 
    public class Program 
    { 
    static void Main(string[] args) 
    { 
     // Display the header and initialize the sample. 
     CommandLine.EnableExceptionHandling(); 
     Console.WriteLine("Create users in a google apps domain!"); 
     Console.WriteLine("by Jonas Bergstedt 2013"); 

     // Get the user data and store in user object 
     Console.Write("Email: "); 
     string userId = Console.ReadLine(); 

     Console.Write("Givenname: "); 
     string GivenName = Console.ReadLine(); 

     Console.Write("Familyname: "); 
     string FamilyName = Console.ReadLine(); 

     Console.Write("Password: "); 
     string Password = Console.ReadLine(); 

     User newuserbody = new User(); 
     UserName newusername = new UserName(); 
     newuserbody.PrimaryEmail = userId; 
     newusername.GivenName = GivenName; 
     newusername.FamilyName = FamilyName; 
     newuserbody.Name = newusername; 
     newuserbody.Password = Password; 

     // Register the authenticator. 
     var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description) 
      { 
       ClientIdentifier = "<your clientId from Google APIs Console>", 
       ClientSecret = "<your clientsecret from Google APIs Console>", 
      }; 

     var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization); 

     // Create the service. 
     var service = new DirectoryService(new BaseClientService.Initializer() 
      { 
       Authenticator = auth, 
       ApplicationName = "Create User", 
       ApiKey = "<your API Key from Google APIs console> (not sure if needed)"    
      }); 

     User results = service.Users.Insert(newuserbody).Execute(); 
     Console.WriteLine("User :" + results.PrimaryEmail + " is created"); 
     Console.WriteLine("Press any key to continue!"); 
     Console.ReadKey(); 
    } 
    private static IAuthorizationState GetAuthorization(NativeApplicationClient arg) 
    { 
     // Get the auth URL: 
     IAuthorizationState state = new AuthorizationState(new[] { DirectoryService.Scopes.AdminDirectoryUser.GetStringValue() }); 
     state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl); 
     Uri authUri = arg.RequestUserAuthorization(state); 

     // Request authorization from the user (by opening a browser window): 
     Process.Start(authUri.ToString()); 
     Console.WriteLine(); 
     Console.Write("Authorization Code: "); 
     string authCode = Console.ReadLine(); 

     // Retrieve the access token by using the authorization code: 
     return arg.ProcessUserAuthorization(authCode, state); 
    } 
} 

}

+0

你好。你使用NuGet包嗎?或者你如何下載Directory API?提前致謝。 –

+0

JoBe我可以通過電子郵件向您發送關於代碼和平的問題嗎? –

+0

@OrlandoHerrera StackOverflow使其故意難以聯繫其他成員,因爲它不是一個社交網站。由於這種私人求助請求是不受歡迎的,除非你問的人表示他願意在你問*之前私下幫助*。但即使在這種情況下,這也不是最好的主意,因爲我們打算在網站上互相幫助。發佈您的問題。讓其他人看到並回答它。讓更多的人從你的問題和他們的答案中學習。這是StackOverflow的方式! =) –

相關問題