使用 Google Workspace API 获取用户使用的存储空间

问题描述 投票:0回答:1

我想查询 Google Workspace API 以获取用户消耗的存储空间。 我正在使用 .NET 7 C# 访问用户的 Google Directory 和 GMAIL 配置。这连接到服务应用程序并使用模拟,并且这一切都正常工作。我已将 DriveService.Scope.Drive 范围添加到我的 Scopes 数组中,并且该范围在我的 Google API 服务中启用。 我正在调用以下函数:

 public void GetUserStorageUsage(string emailAddress)
{
    // Create Directory API service.
    DriveService driveService = new DriveService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = GetOAuthServiceAccountCredential(emailAddress),
        ApplicationName = ApplicationName
    });



    var x = driveService.About.Get();
    if(x !=null)
    {
        var z = x.Execute();
    }

}

它从 Google.Apis 包中抛出以下异常:

Google.GoogleApiException
  HResult=0x80131500
  Source=Google.Apis
  StackTrace:
   at Google.Apis.Requests.ClientServiceRequest`1.<ParseResponse>d__38.MoveNext() in Google.Apis.Requests\ClientServiceRequest.cs:line 157
   at Google.Apis.Requests.ClientServiceRequest`1.Execute() in Google.Apis.Requests\ClientServiceRequest.cs:line 103

我在 ClientServiceRequest.cs 中添加了一个断点,以便我可以查看 HttpResponseMessage 并调用:

https://www.googleapis.com/drive/v3/about

我收到 400 状态代码,原因短语为“错误请求”

也许我的做法是错误的? 如果有人有见识,我将不胜感激。 我没能找到获取 Google Workspace 用户存储空间使用情况的示例。

这里是模拟功能

public ServiceAccountCredential GetOAuthServiceAccountCredential(String IpersonatedUser)
{
           ServiceAccountCredential credential;

           using (FileStream? stream = new FileStream(Filename , FileMode.Open, FileAccess.Read))
           {
               credential =
                 GoogleCredential
                   .FromStream(stream)
                   .CreateScoped(Scopes)
                   .CreateWithUser(IpersonatedUser)
                   .UnderlyingCredential as ServiceAccountCredential;
           }
           //credential.User = IpersonatedUser;

           //FieldInfo userField = typeof(ServiceAccountCredential).GetField("User", BindingFlags.NonPublic | BindingFlags.Instance);
           //userField.SetValue(credential, IpersonatedUser);

           return credential;
}

以下是范围:

private String[] Scopes = {
    DirectoryService.Scope.AdminDirectoryCustomer,
    DirectoryService.Scope.AdminDirectoryDomain,
    DirectoryService.Scope.AdminDirectoryGroup,
    DirectoryService.Scope.AdminDirectoryGroupMember,
    DirectoryService.Scope.AdminDirectoryOrgunit,
    DirectoryService.Scope.AdminDirectoryUser,
    DirectoryService.Scope.AdminDirectoryUserAlias,
    GmailService.Scope.GmailSettingsBasic,
    GmailService.Scope.GmailSettingsSharing,
    DriveService.Scope.Drive
};

谢谢

c# .net-core google-api google-workspace
1个回答
0
投票

这是我的驾驶服务帐户样本

using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;

Console.WriteLine("Google drive api service account example, workspace delegation");

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

var credential = GoogleCredential.FromFile(PathToServiceAccountKeyFile)
    .CreateWithUser("[email protected]")  // delegate to user on workspace.
    .CreateScoped(new[] {DriveService.ScopeConstants.Drive});

// Create the  Drive service.
var service = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential
});

var result = await service.Files.List().ExecuteAsync();

foreach (var file in result.Files)
{
    Console.WriteLine(file.Id);
}

关于.获取

about .net 要求您传递字段 var

var request = service.About.Get();
    request.Fields = "*";
var aboutResponse = await request.ExecuteAsync();    

var json = JsonSerializer.Serialize(aboutResponse);
Console.WriteLine(json);
© www.soinside.com 2019 - 2024. All rights reserved.