几天来,我一直在寻找这方面的信息.我相信我已经为访问正确设置了与API相关的所有内容,但我一直收到这个错误: 服务管理员引发了异常.

HttpStatusCode is BadRequest.
Google.Apis.Requests.RequestError
Bad Request [400]
Errors [
        Message[Bad Request] Location[ - ] Reason[badRequest] Domain[global]
]

Google.GoogleApiException: The service admin has thrown an exception. HttpStatusCode is BadRequest. Bad Request
   at Google.Apis.Requests.ClientServiceRequest`1.ParseResponse(HttpResponseMessage response)
   at Google.Apis.Requests.ClientServiceRequest`1.ExecuteAsync(CancellationToken cancellationToken)
   at Google.Apis.Requests.ClientServiceRequest`1.ExecuteAsync()

我运行代码的方法:

string[] googleScopes = {DirectoryService.Scope.AdminDirectoryUser};
public async Task connectToGoogle()
        {
            try
            {
                GoogleCredential credential = GoogleCredential.FromFile(googleTokenPath).CreateScoped(googleScopes);//.CreateWithUser(googleAdminEmail);
                
                
                var service = new DirectoryService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "TestAPIProject"
                });
                
                var request = service.Users.List();
                request.Customer = "test.example.com";
                request.Domain = "test.example.com";
                var result = await request.ExecuteAsync();
                
                
                Console.WriteLine(result);
                
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e);
            }
        }

据我所知,JSON令牌在这方面是正确的.我认为问题出在客户或域名上,但我已经try 了很多不同的 Select ,仍然得到了Bad Request.

我正在使用Google Workspace服务帐户发送请求.确保使用该令牌,而不是OAuth令牌.

推荐答案

首先,您注释掉了模拟所需的CreateWithUser.在那之后,我会判断你的客户,以确保是正确的.

using Google.Apis.Auth.OAuth2;
using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Services;

Console.WriteLine("Hello, Google Calendar Workspace sample!");

var scopes = new[] { DirectoryService.Scope.AdminDirectoryUser };

const string workspaceAdmin = "user@yourdomain.com";

const string credentials = @"C:\Development\FreeLance\GoogleSamples\Credentials\workspaceserviceaccount.json";

var credential = GoogleCredential.FromFile(credentials).CreateScoped(scopes).CreateWithUser(workspaceAdmin);

var services = new DirectoryService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
});

var request = services.Users.List();
request.Customer = "my_customer";
request.MaxResults = 10;
request.OrderBy = UsersResource.ListRequest.OrderByEnum.Email;
    
var results = request.Execute();

var users = results.UsersValue;

if (users.Count == 0)
{
    Console.WriteLine("No Users");
    return;
}

Console.WriteLine("Users:");
foreach (var user in users)
{
    Console.WriteLine($"{user.PrimaryEmail} ({user.Name.FullName})");
}

Csharp相关问答推荐

Blazor:用参数创建根路径

C#将参数传递给具有变化引用的变量

LINQ无法翻译SQLFunctions方法

EF Core在请求列表时忽略列,但在按ID获取时包含

如何使用CsvReader获取给定列索引的列标题?

从应用程序图API调用访问所有者字段

自动映射程序在GroupBy之后使用项目

如何将不同类型的扩展参数的javascript函数转换成C#风格?

Rider将.NET安装在哪里

如何使用.NET6WPF打印车票?

如何从Entity Framework Core中填充ListIInterface

C# Winforms:从对象树到TreeView的递归转换重复条目

在.NET Maui中,Flyoutindow/Hamburger菜单可以在shell 之外实现吗?

如何使用IHostedService添加数据种子方法

Xamarin.Forms中具有类似AspectFill的图像zoom 的水平滚动视图

使用DI实例化带有动态参数的服务?

CsvHelper在第二次迭代时抛出System.ObjectDisposedException

自定义ConsoleForMatter中的DI/Http上下文

C#Web服务转换为 node /Express不工作

将ValueTask发送给调用者