0

I'm creating a simple app that changes vacation settings for multiple g-suite users. Currently, I'm creating GoogleCredential and GmailService objects per mailbox, like so:

var credentials = GoogleCredential.FromFile(FileName).CreateScoped(Scopes).CreateWithUser(User);

var gmailService = new GmailService(
    new BaseClientService.Initializer()
    {
        ApplicationName = ApplicationName,
        HttpClientInitializer = credentials
    });

var x = gmailService.Users.GetProfile(User);
var y = await x.ExecuteAsync();

Console.WriteLine(y.MessagesTotal);

long now = (long)DateTime.UtcNow.Subtract(
    new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
).TotalMilliseconds;



VacationSettings settings = new VacationSettings
{
    EnableAutoReply = true,
    RestrictToDomain = false,
    ResponseBodyPlainText = "I'm on vacations :)",
    ResponseSubject = "Vacations",
    StartTime = now

};

UsersResource.SettingsResource.UpdateVacationRequest request = gmailService.Users.Settings.UpdateVacation(settings, "me");

var response = await request.ExecuteAsync();

The above code works fine, but I'm trying to reuse it for multiple mailboxes. I've tried removing CreateWithUser(User) when creating GoogleCredential and calling API with specific email instead of "me", like so:

var req1 = gmailService.Users.Settings.UpdateVacation(settings, "a@example.com").Execute();
var req2 = gmailService.Users.Settings.UpdateVacation(settings, "b@example.com").Execute();

but I get below error:

Google.GoogleApiException
HResult=0x80131500
Message=Google.Apis.Requests.RequestError
Precondition check failed. [400]
Errors [
Message[Precondition check failed.] Location[ - ] Reason[failedPrecondition] Domain[global]
]

so I need some special scopes for my service account? I asked this question in google API repo on GitHub, but I was suggested to ask it here to get an answer from Gmail API Team.

Is this even possible?

Misiu
  • 4,738
  • 21
  • 94
  • 198
  • What line throws this error? – Rafa Guillermo Aug 24 '20 at 10:47
  • @RafaGuillermo `await request.ExecuteAsync();` Question is: do I need some special permission to modify other user's mailboxes? OR do I must impersonate every request? – Misiu Aug 24 '20 at 13:04
  • You must impersonate every request individually. The delegated credentials are build with the subject included. Check out [this](https://stackoverflow.com/a/61932919/11551468) - it's node.je instead of C# but you can see how the creds are built. – Rafa Guillermo Aug 24 '20 at 13:05
  • @RafaGuillermo I'm doing that right now, but I was hoping I can impersonate as admin and settings for many mailboxes at once. I've posted the same question on GitHub and have answer from the team: https://github.com/googleapis/google-api-dotnet-client/issues/1629#issuecomment-678326541 – Misiu Aug 24 '20 at 13:25
  • 1
    You can't, it has to be one user at a time, an even as an admin you don't have mailbox rights for other domain users - you have to impoersonate the owner. – Rafa Guillermo Aug 24 '20 at 13:38

0 Answers0