Gmail API Error 400 - Precondition check failed happens inconsistently

Viewed 1018

I'm struggling with a Gmail API issue. A few things to get started:

  • Using OAuth2
  • My account is GSuite enabled
  • Scope includes Modify
  • My program is in GoLang

My program is sending an email on behalf of an authed user. Nearly all Gmail API functionality works perfectly - I'm able to create a draft, send an email without creating a draft, operate on labels, etc.

The issue seems to occur only when I attempt to send a draft object using the users.drafts.send method: https://developers.google.com/gmail/api/reference/rest/v1/users.drafts/send

When I create a draft message and then fire off the request to send the message, I inconsistently get back error 400 Precondition check failed.

This is a POST request to the endpoint https://gmail.googleapis.com/gmail/v1/users/me/drafts/send?uploadType=multipart

The weird part leaving me completely baffled is that I can make an identical request directed at a different draft message ID using the same OAuth access token half a second later and it works perfectly.

This request failed - timestamp 2022-01-04T19:43:50.278Z

Method:POST URL:https://gmail.googleapis.com/gmail/v1/users/me/drafts/send?uploadType=multipart Proto:HTTP/1.1 ProtoMajor:1 ProtoMinor:1 Header:map[Authorization:[Bearer ya29.a0ARrdaM9mREDACTED] Content-Type:[application/json]] Body:{Reader:0xc0004e7dd0} GetBody:0x7198e0 ContentLength:70 TransferEncoding:[] Close:false Host:gmail.googleapis.com Form:map[] PostForm:map[] MultipartForm:<nil> Trailer:map[] RemoteAddr: RequestURI: TLS:<nil> Cancel:<nil> Response:<nil> ctx:0xc000044060

Body object: {"id":"r155747228619414494","message":{"threadId":"17e269e7bb12bece"}}

And this request succeeded - timestamp 2022-01-04T19:43:50.230Z

Method:POST URL:https://gmail.googleapis.com/gmail/v1/users/me/drafts/send?uploadType=multipart Proto:HTTP/1.1 ProtoMajor:1 ProtoMinor:1 Header:map[Authorization:[Bearer ya29.a0ARrdaM9mREDACTED] Content-Type:[application/json]] Body:{Reader:0xc00061b920} GetBody:0x7198e0 ContentLength:70 TransferEncoding:[] Close:false Host:gmail.googleapis.com Form:map[] PostForm:map[] MultipartForm:<nil> Trailer:map[] RemoteAddr: RequestURI: TLS:<nil> Cancel:<nil> Response:<nil> ctx:0xc000044060

Body object: {"id":"r107084306592061046","message":{"threadId":"17e269e82b321030"}}

I am capturing errors for scenarios like rate-limiting - the error in question is very specifically Precondition check failed., and I have only seen it so far using this specific endpoint.

Everything I've found online suggests this error points to an issue with auth. I have authed, un-authed, and re-authed, all with no difference.

I've also gone back into the developer page above and used the Gmail test tool to send the same request body values, and the draft sends successfully, which suggests this probably isn't a corrupted draft message.

All that context to ask 2 questions:

  1. Has anyone seen a Gmail error where that error message indicates something I may not be considering? The Gmail documentation has nothing I can see about that error message.
  2. Does anyone have an idea why this is happening inconsistently with requests that are essentially identical?

Thanks!

0 Answers
Related