使用谷歌对.NET目录API? [英] Using Google's Directory API for .Net?
问题描述
我想使用谷歌的目录API,用于.NET创建我的第一个控制台应用程序。
I am trying to create my first console app using Google's Directory API for .Net.
我有一个基于谷歌的样品中的code。它显示了我一对夫妇的错误,其中之一是,当我试图创建服务:
I have a code based in a Google's sample. It shows me a couple of errors, one of them is when I am trying to create the service:
var service = new DirectoryService(new BaseClientService.Initializer()
{
Authenticator = auth,
ApplicationName = "Create User",
ApiKey = "<your API Key from Google APIs console>"
});
它显示我:错误3Google.Apis.Services.BaseClientService.Initializer'不包含'身份验证'的定义
It shows me: "Error 3 'Google.Apis.Services.BaseClientService.Initializer' doesn't contain a definition for 'Authenticator'"
和在第二的错误是在这个函数
And the second error is in this function
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg){}
它显示我:DotNetOpenAuth.OAuth2.UserAgentClient在未被引用的程序集中定义。
It shows me: "DotNetOpenAuth.OAuth2.UserAgentClient' is defined in an assembly that is not referenced."
在这种情况下,我输入(使用的NuGet控制台):PM>安装,包装DotNetOpenAuth -Version 4.3.4.13329 ....但它不解决我的问题。
In this case I typed (using nuget console): PM> Install-Package DotNetOpenAuth -Version 4.3.4.13329 .... but it doesnt resolve my problem.
这是我的code:
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);
}
}
}
在此先感谢您的帮助
推荐答案
必须有一个服务帐户中定义的执行权力机构的谷歌企业应用套件域范围代表团
have to have a service account with Perform Google Apps Domain-Wide Delegation of Authority as defined in
https://developers.google.com/admin-sdk/目录/ V1 /引导/代表团
说给它添加到管理第三方OAuth的客户端访问我有管理OAuth客户端访问
it say to add it in the "Manage third party OAuth Client access" i had "Manage OAuth Client access "
String serviceAccountEmail = "......@developer.gserviceaccount.com";
X509Certificate2 certificate = new X509Certificate2(@"C:\key.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[]
{
DirectoryService.Scope.AdminDirectoryUser
},
User = "admin@domain.com"
}.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 = "jack@domain.com";
newusername.GivenName = "jack";
newusername.FamilyName = "black";
newuserbody.Name = newusername;
newuserbody.Password = "password";
User results = ser.Users.Insert(newuserbody).Execute();
这篇关于使用谷歌对.NET目录API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!