I'm trying to create drafts for users using a service account. So I've created a service account in the developer console and gave it permissions to access the gmail compose api. I found this example: https://google-developers.appspot.com/api-client-library/dotnet/guide/aaa_oauth?hl=lv#service_account and changed it to use the gmail compose api like this:
var certificate = new X509Certificate2(_privateKey2, "notasecret",X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(_emailAddressService)
{
Scopes = new[] {GmailService.Scope.GmailCompose}
}.FromCertificate(certificate));
// Create the service.
var service = new GmailService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Plus API Sample",
});
var draft = new Draft();
draft.Message = new Message();
draft.Message.Payload = new MessagePart();
draft.Message.Payload.Body = new MessagePartBody();
draft.Message.Payload.Body.Data = "";
draft = service.Users.Drafts.Create(draft, "someuser@domain.com").Execute();
This should create an empty draft, I've tested it with the API explorer on https://developers.google.com/gmail/api/v1/reference/users/drafts/create. However, all I get back is a backend error without any useful information:
Google.Apis.Requests.RequestError
Backend Error [500]
Errors [
Message[Backend Error] Location[ - ] Reason[backendError] Domain[global]
]
I've checked the request I send using Fiddler and the it looks like this: {"message":{"payload":{"body":{"data":""}}}}, exactly the same as the request I did with the API explorer. What am I doing wrong here? Any help is very much appreciated.
Update: I also tried
service.Users.Drafts.List("user@2domain.com").Execute();
and
messages = service.Users.Messages.List("user@doamin.com").Execute();
and I get the same error message. It looks like I can't access any api?