2014-02-07 26 views
1

我正在嘗試使用Google的Directory API for .NET創建我的第一個控制檯應用程序。使用Google的Directory API for .Net?

我有一個基於Google樣本的代碼。它顯示了我一對夫婦的錯誤,他們的一個是當我試圖創建服務:

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

它顯示了我:「錯誤3‘Google.Apis.Services.BaseClientService.Initializer’沒有按」噸包含用於定義「身份驗證器」「

第二誤差是在該功能

private static IAuthorizationState GetAuthorization(NativeApplicationClient arg){} 

它顯示我:」 DotNetOpenAuth.OAuth2.UserAgentClient」是在assemb定義這是沒有引用。「

在這種情況下,我輸入(使用nuget控制檯):PM> Install-Package DotNetOpenAuth -Version 4.3.4.13329 ....但它不能解決我的問題。


這是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 


using System.Diagnostics; 

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.directory_v1; 
using Google.Apis.Admin.Directory.directory_v1.Data; 


namespace GoogleDirectoryApi_test02_consola 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
     String CLIENT_ID = "YOUR_CLIENT_ID"; 
     String CLIENT_SECRET = "YOUR_CLIENT_SECRET"; 

     // Register the authenticator and create the service 
     var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET); 
     var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization); 

     //New User 
     User newuserbody = new User(); 
     string userId = "SampleId01"; 
     UserName newusername = new UserName(); 
     newuserbody.PrimaryEmail = userId; 

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


     User results = service.Users.Insert(newuserbody).Execute(); 
    } 



    private static IAuthorizationState GetAuthorization(NativeApplicationClient arg) 
    { 
     // Get the auth URL: 
     //IAuthorizationState state = new AuthorizationState(new[] { DirectoryService.Scopes.AdminDirectoryUser.GetStringValue() }); 
     IAuthorizationState state = new AuthorizationState(new[] { DirectoryService.Scope.AdminDirectoryUser.ToString() }); 
     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); 
     } 
    } 
} 

預先感謝您的幫助

回答

2

必須有一個與中定義的執行局的谷歌應用程序域範圍代表團服務帳戶

https://developers.google.com/admin-sdk/directory/v1/guides/delegation

它說要添加它在「管理第三方OAuth客戶端訪問」我有「管理OAuth客戶端訪問」

String serviceAccountEmail = "[email protected]"; 
       X509Certificate2 certificate = new X509Certificate2(@"C:\key.p12", "notasecret", 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 = "Get it to work", 
       }); 

       User newuserbody = new User(); 
       UserName newusername = new UserName(); 
       newuserbody.PrimaryEmail = "[email protected]"; 
       newusername.GivenName = "jack"; 
       newusername.FamilyName = "black"; 
       newuserbody.Name = newusername; 
       newuserbody.Password = "password"; 

       User results = ser.Users.Insert(newuserbody).Execute(); 
相關問題