I've been trying to use a service account in order to obtain google workspace activities via the admin SDK but although the service account is granted an admin role with Reports privilidges I'm receiving the following error -
Access denied. You are not authorized to read activity records. [401] Errors [ Message[Access denied. You are not authorized to read activity records.] Location[Authorization - header] Reason[authError] Domain[global] ]
See the following code snippet -
using Google.Apis.Admin.Reports.reports_v1;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
var scopes = new[]
{
ReportsService.ScopeConstants.AdminReportsAuditReadonly,
};
var serviceAccountMail = "<ServiceAccountMail>";
var serviceAccountPrivateKey = "<PrivateKey>";
var serviceAccountCredential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountMail).FromPrivateKey(serviceAccountPrivateKey)) { Scopes = scopes };
var reportsService = new ReportsService(new BaseClientService.Initializer { HttpClientInitializer = serviceAccountCredential });
var listActivitiesRequest = reportsService.Activities.List("all", ActivitiesResource.ListRequest.ApplicationNameEnum.Token);
var activities = await listActivitiesRequest.ExecuteAsync();
foreach (var activity in activities.Items)
{
Console.WriteLine(activity);
}
For comparison the following code snippet works as expected with the same setup -
using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
var scopes = new[]
{
DirectoryService.ScopeConstants.AdminDirectoryUserReadonly
};
var serviceAccountMail = "<ServiceAccountMail>";
var serviceAccountPrivateKey = "<PrivateKey>";
var serviceAccountCredential =
new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountMail).FromPrivateKey(serviceAccountPrivateKey))
{ Scopes = scopes };
var directoryService = new DirectoryService(new BaseClientService.Initializer { HttpClientInitializer = serviceAccountCredential });
var listUsersRequest = directoryService.Users.List();
listUsersRequest.Customer = "<CustomerId>";
var users = await listUsersRequest.ExecuteAsync();
foreach (var user in users.UsersValue)
{
Console.WriteLine(user);
}
See the following custom admin role assigned to the service account -