一段时间以来,我一直在尝试列出我的谷歌应用程序域中的用户。在 Python 中没有问题,但在 C# 中我收到一条错误消息:发生错误:Google.Apis.Requests.RequestError Bad Request [400] Errors [ Message[Bad Request] Location[ - ] Reason[badRequest] Domain[global] ]
我不是任何类型的 C# 专家,但是当我查看 Google.Apis.Admin.directory_v1.cs - 文件时,它看起来好像 UserResource ListRequest 是错误的???它位于文件的第 7349-7352 行。任何人都知道它是否尚未在 API 中实现?
编辑:我从为什么我认为 Google.Apis.Admin.directory_v1.cs中的代码,第 7349-7352 行是错误的开始(正如我所提到的 - 我不是 C#-guru):
编码:
/// <summary>Retrieve either deleted users or all users in a domain (paginated)</summary>
public virtual ListRequest List() {
return new ListRequest(service);
}
为什么我觉得很奇怪:
我无法看到将 customerid 或域作为参数传递给此请求的位置,但在 APIs Explorer 中它是必需的(否则我在原始帖子中收到与上述相同的错误消息)。
编辑:我在文件中往下看了一点,我猜第 8904 行及以后的行正在做我之前寻找的事情。我的错!
但我仍然无法让我的代码工作?!?!?
我的代码不起作用:
static void Main(string[] args)
{
// Display the header and initialize the sample.
CommandLine.EnableExceptionHandling();
Console.WriteLine("List users in a google apps domain!");
Console.WriteLine("by Jonas Bergstedt 2013");
// Get the domainname
Console.Write("Domain: ");
string domain = Console.ReadLine();
// Register the authenticator.
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
{
ClientIdentifier = <myClientId>,
ClientSecret = <myClientSecret>",
};
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
// Create the service.
var service = new DirectoryService(new BaseClientService.Initializer()
{
Authenticator = auth,
ApplicationName = "List Users",
ApiKey = <myApiKey>
});
// Trying to add the domain
service.Users.List().Domain = domain;
Users results = service.Users.List().Execute();
foreach (User list in results.UsersValue)
{
Console.WriteLine("- " + list.Name);
}
}
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);
}
}