C# 服务帐户-Google Drive的域范围授权失败

C# 服务帐户-Google Drive的域范围授权失败,c#,google-drive-api,google-authentication,google-workspace,C#,Google Drive Api,Google Authentication,Google Workspace,我正在尝试设置服务帐户和域权限。我已成功创建了服务帐户,并已授予该域的权限 我已为服务帐户启用了所有必需的作用域: 在一段代码中,我可以使用我们的域用户的日历或邮件,但不能使用驱动器 我正在运行以下代码: public static readonly string[] REQUIRED_PERMISSIONS = { CalendarService.Scope.Calendar, "https://www.google.com/m8/feeds/contacts", Task

我正在尝试设置服务帐户和域权限。我已成功创建了服务帐户,并已授予该域的权限

我已为服务帐户启用了所有必需的作用域:

在一段代码中,我可以使用我们的域用户的日历或邮件,但不能使用驱动器

我正在运行以下代码:

public static readonly string[] REQUIRED_PERMISSIONS =
{
   CalendarService.Scope.Calendar,
   "https://www.google.com/m8/feeds/contacts",
   TasksService.Scope.Tasks,
   DriveService.Scope.Drive,
   DriveService.Scope.DriveAppsReadonly,
   DriveService.Scope.DriveFile,
   DriveService.Scope.DriveAppdata,
   DriveService.Scope.DriveMetadataReadonly,
   DriveService.Scope.DriveReadonly,
   GmailService.Scope.MailGoogleCom
};

var cred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer("user.iam.gserviceaccount.com")
{
    Scopes = REQUIRED_PERMISSIONS,
    User = "test@domain"
}.FromPrivateKey(@"privatekey"));

var mailService = new GmailService(new BaseClientService.Initializer()
{
    HttpClientInitializer = cred,
    ApplicationName = ApplicationName,
});

var result = mailService.Users.GetProfile("me");

var resultFetch = result.Execute();

我得到错误
Google.api.Auth.OAuth2.Responses.TokenResponseException:'error:'unauthorized_client',Description:'client未经授权使用此方法检索访问令牌,或者client未经授权使用任何请求的作用域。“,Uri:'

我知道我正在使用GMail请求,但这只是为了“测试连接”,稍后将使用驱动器请求

当我删除与驱动器相关的作用域时,所有的作用域都在工作-然而,这不是解决方案

我有:

  • 已启用域范围的权限
  • 我已创建服务帐户
  • 我已启用驱动器API
  • 我也试过这些问题:
    您的问题是因为您使用了太多的范围,并且这些范围与权限重叠。我的意思是
    DriveService.Scope.Drive
    将允许您执行任何操作(读取/创建/写入/删除),同时
    DriveService.Scope.driveappsreadoly
    将只允许您执行读取操作(获取/列出),因此它将剥夺您事先设置的权限

    我建议您仅使用
    DriveService.Scope.Drive
    作用域进行测试,然后根据需要限制更多权限。另外,不要忘记更改ManageAPI客户端访问中的作用域

    您可以检查所有驱动器API作用域