服务帐户-Google云端硬盘无法访问的域范围权限

时间:2019-12-09 13:44:58

标签: c# google-drive-api google-authentication gsuite

我正在尝试设置服务帐户和域范围的权限。我已经成功创建了服务帐户,并且还委派了整个域的权限。

我已经启用了服务帐户的所有必需范围: Required scopes

通过代码,我可以使用我们域用户的日历或邮件,但不能使用驱动器。

我正在运行以下代码:

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.Apis.Auth.OAuth2.Responses.TokenResponseException: 'Error:"unauthorized_client", Description:"Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.", Uri:""'

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

当我删除与Drive相关的示波器时,所有示波器都在工作-但这不是解决方案。

我有:

  1. 已启用域范围的权限
  2. 我已创建服务帐户
  3. 我已启用Drive API

我也尝试过以下问题: Access Domain wide Google Drive data with ServiceAccount Actor user Failure of delegation of Google Drive access to a service account

1 个答案:

答案 0 :(得分:0)

您的问题是由于您使用的 范围 过多,并且它们的权限重叠。我的意思是,DriveService.Scope.Drive允许您执行任何操作(读取/创建/写入/删除),而DriveService.Scope.DriveAppsReadonly仅允许您执行读取操作(获取/列出),因此它将剥夺您预先设置的权限。

我建议您仅将DriveService.Scope.Drive范围用于测试目的,然后根据需要限制更多权限。另外,不要忘记在您的Manage API客户端访问中更改范围。

您可以检查所有Drive API范围HERE

相关问题