2

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?

David Libido
  • 469
  • 2
  • 12
  • 1
    I think you and I are having the same issue ([link](https://stackoverflow.com/questions/24523003/error-500-backenderror-with-gmail-api-and-google-apis-node-client)). My current thought is that the Gmail API doesn't actually support service account users. Whenever I try to add the "sub" field (see ["Additional Claims"](https://developers.google.com/accounts/docs/OAuth2ServiceAccount)), I get an "access denied" error instead of a 500 error. – Ryan Jul 03 '14 at 18:49
  • 1
    Yes, it looks like it. Would be nice to have someone from Google confirm this. – David Libido Jul 04 '14 at 07:29
  • This indicates otherwise: http://stackoverflow.com/questions/24779138/can-we-access-gmail-api-using-service-account/24795241#24795241 – o1lo01ol1o Feb 26 '16 at 19:30

0 Answers0