【发布时间】: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 请求,但这只是为了“测试连接”,稍后将使用驱动器请求
当我删除与云端硬盘相关的范围时,一切正常 - 但这不是解决方案。
我有:
- 已启用域范围的权限
- 我已创建服务帐户
- 我已启用 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