【问题标题】:Service account - domain-wide authority failing for Google Drive服务帐户 - Google Drive 的域范围权限失败
【发布时间】:2020-04-02 15:13:43
【问题描述】:

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

我已为服务帐户启用所有必需的范围:

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

我正在运行以下代码:

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 请求,但这只是为了“测试连接”,稍后将使用驱动器请求

当我删除与云端硬盘相关的范围时,一切正常 - 但这不是解决方案。

我有:

  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

【问题讨论】:

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


    【解决方案1】:

    您的问题是由于您使用了太多 范围 并且它们的权限重叠。我的意思是DriveService.Scope.Drive 将允许您执行任何操作(读取/创建/写入/删除),同时DriveService.Scope.DriveAppsReadonly 将只允许您执行读取操作(获取/列表),因此它将带走您事先设置的权限。

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

    您可以检查所有 Drive API Scopes HERE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 2017-06-29
      相关资源
      最近更新 更多