0
votes

I want to update signature of a domain user via Gmail .Net Client Library as G Suite administrator.

I followed this example to setup and give permissions in google developer console.

Here is the summary what I did:

  1. Enabled Gmail API in Google API Console
  2. Created a Service Account for my project
  3. Delegated domain-wide authority to the Service Account
  4. Added https://www.googleapis.com/auth/gmail.settings.sharing, https://www.googleapis.com/auth/gmail.settings.basic scopes

I'm able to update my own email signature but when I try to update signature of any other user on my domain I get following error:

Google.Apis.Requests.RequestError
Not Found [404]
Errors [
    Message[Not Found] Location[ - ] Reason[notFound] Domain[global]
]

Here's my code:

GoogleCredential credential;
using (var stream = new FileStream("signature-gmailapi.json", FileMode.Open, FileAccess.Read))
{
    credential = GoogleCredential.FromStream(stream).CreateScoped(new[]
            {GmailService.Scope.GmailSettingsBasic, GmailService.Scope.GmailSettingsSharing})
        .CreateWithUser("[email protected]");
}
var service = new GmailService(new BaseClientService.Initializer()
    {
        ApplicationName = "Project",
        HttpClientInitializer = credential
    }
);
service.Users.Settings.SendAs.Patch(new SendAs { Signature = "Test signature..."}, "me",
    "[email protected]").Execute();

Can you anyone please guide me what I' doing wrong?

1

1 Answers

1
votes

To change a domain user's signature with the service account, you need to use impersonation

This is achieved with the method CreateWithUser();

The parameter you need to assign to it, is the USER's email, not the admin's one. So:

credential = GoogleCredential.FromStream(stream).CreateScoped(new[]{GmailService.Scope.GmailSettingsBasic, GmailService.Scope.GmailSettingsSharing})
        .CreateWithUser( "[email protected]");