You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google Workspace API获取用户存储用量遇400错误求助

解决Google Workspace API获取用户存储用量的400错误问题

第一步:捕获详细错误信息

GoogleApiException包含具体的错误原因,先修改代码捕获并输出,明确400错误的根源:

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

    var x = driveService.About.Get();
    if(x != null)
    {
        try
        {
            var z = x.Execute();
        }
        catch (Google.GoogleApiException ex)
        {
            Console.WriteLine($"API Error: {ex.Message}");
            foreach (var err in ex.Error.Errors)
            {
                Console.WriteLine($"Error Reason: {err.Reason}, Detail: {err.Message}");
            }
        }
    }
}

正确获取用户存储用量的两种方案

方案1:通过Admin SDK Directory API获取总存储用量(推荐)

该接口返回用户在Google Workspace所有服务(Drive、Gmail、Photos等)的总已用存储量,无需调用Drive API:

  1. 确保权限数组包含DirectoryService.Scope.AdminDirectoryUser(或AdminDirectoryUserReadonly,更安全)
  2. 实现代码:
public long GetUserTotalStorage(string userEmail)
{
    var directoryService = new DirectoryService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = GetOAuthServiceAccountCredential(userEmail),
        ApplicationName = ApplicationName
    });

    // 仅请求存储用量字段,减少响应数据量
    var userRequest = directoryService.Users.Get(userEmail);
    userRequest.Fields = "quotaBytesUsed";

    var userData = userRequest.Execute();
    return userData.QuotaBytesUsed ?? 0;
}

方案2:通过Drive API仅获取Drive存储用量

如果只需要Drive部分的存储用量,需注意指定返回字段(默认接口不会返回存储配额信息):

  1. 将权限数组中的DriveService.Scope.Drive替换为更安全的DriveService.Scope.DriveMetadataReadonly
  2. 实现代码:
public long GetUserDriveStorage(string userEmail)
{
    var driveService = new DriveService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = GetOAuthServiceAccountCredential(userEmail),
        ApplicationName = ApplicationName
    });

    var aboutRequest = driveService.About.Get();
    // 明确指定需要存储配额相关字段
    aboutRequest.Fields = "storageQuota";

    try
    {
        var aboutData = aboutRequest.Execute();
        return aboutData.StorageQuota.Used ?? 0;
    }
    catch (Google.GoogleApiException ex)
    {
        Console.WriteLine($"Drive API Error: {ex.Message}");
        foreach (var err in ex.Error.Errors)
        {
            Console.WriteLine($"Reason: {err.Reason}, Detail: {err.Message}");
        }
        return 0;
    }
}

关键配置检查

  • 确保服务账号已在Google Workspace管理控制台开启域范围委派
  • 确保在管理控制台的「API权限」中,已为服务账号授权对应Scope(比如https://www.googleapis.com/auth/admin.directory.user.readonly或https://www.googleapis.com/auth/drive.metadata.readonly)
  • 模拟的用户账号需具备对应权限:若获取其他用户的存储,需模拟Workspace管理员账号;若仅获取自身存储,普通用户账号即可

内容的提问来源于stack exchange,提问作者William Holmes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 09:25:07