1

So I have written a Google App Script that will use the Admin SDK - Directory API to get a list of about 80 users. I then take each user and create a Email template with their directory data and then use the Gmail API to set their signature to a company default https://github.com/MrCaspan/GAS-Gmail-Signature-Templator if you care to look at the code. We run the script once an hour so that should be about 24 calls to the Directory API (No issues there) and about 1920 calls to the Gmail API in a 24 hour span (24 x 80 users) But I keep getting this error about 7PM every night and only for a few hours then it goes away

{
  "error": {
    "code": 429,
    "message": "User-rate limit exceeded.  Retry after 2021-10-21T23:45:03.158Z",
    "errors": [
      {
        "message": "User-rate limit exceeded.  Retry after 2021-10-21T23:45:03.158Z",
        "domain": "global",
        "reason": "rateLimitExceeded"
      }
    ],
    "status": "RESOURCE_EXHAUSTED"
  }
}

When I look at my Google Cloud Project and look at my API usage its less then 0.01% of the daily limit. I cannot for the life of me figure out what is causing this issue. Because I am using Google App Script my script runs slow, maybe 180 seconds to finish for 80 users so its about a call to the API every 2 seconds so I cannot be overloading it. Also I can always run the script again and again and it always fails at the same point on the 75th API call 1 call the the Admin SDK and 73 calls successfully to the Gmail API then on the 75th I get this error. Strang thing is that it works perfectly fine all day with no issues its not till later at night that it happens.

Edit To Add: This happens always on the same users and normally happens after setting their signature 18 times and wont let me set it again for 8 hours. So I get a token from Google impersonating the user then set their signature.

MrCaspan
  • 21
  • 4
  • Just as a note I found this https://developers.google.com/gmail/api/reference/quota that saus a Gmail.sendAs.update costs 100 Quota units and you cannot exceed more than 250/second so I added a Utilities.sleep(3000) after each call to see if that fixed it but still failed on the exact same place after 74 calls. – MrCaspan Oct 22 '21 at 01:01
  • Have a look at https://stackoverflow.com/questions/57999440/gmail-api-user-rate-limited-over-extended-period-of-time https://stackoverflow.com/questions/43032692/gmail-api-error-429-ratelimitexceeded-even-where-is-no-any-activity https://stackoverflow.com/questions/63573328/gmail-api-gives-user-rate-limit-exceeded-error – Kos Oct 22 '21 at 03:34
  • Thanks for the reply, I have seen these posts and already tried what they stated. I have reduced my calls to only 1 every 3 seconds and still got this error. I am setting a signature BTW not sending an email but the cost is 100 quota units and I'm allowed 250/second/user and each call to the API is by a different user IE I want to set Bob's signature so I impersonate Bob and set their signature. So i should never hit that quota limit anyway. – MrCaspan Oct 22 '21 at 12:50
  • So still having issues and it always happens with 1 users usually about 3/4 of the way through a 24 hour day. Blocks the users from setting their signature for about 8 hours. What is this limit I am hitting and how do I prevent it? I could just skip over the user but I want to know why this is happening. – MrCaspan Oct 24 '21 at 03:39
  • Which, this I suppose `Per user rate limit 250 quota units per user per second, moving average (allows short bursts).` this the first thing in documentation – Kos Oct 24 '21 at 07:24
  • Let me ask a better question. If I request a bearer token and I impersonate a user to get it and then I use that bearer token to access the API and set the users signature. Question: what user does the quota count against? I am assuming the user that I am impersonating. This 250 quota says it's a per user quota. If that is the case then how is it possible to hit this limit if the script is only called once per HOUR? – MrCaspan Oct 24 '21 at 13:24
  • user quota is spread across all scripts, maybe you have another application authorized which is why it doesn't have to be blamed on your script – Kos Oct 24 '21 at 15:25
  • 1
    This user has not even logged into their account yet and this so the only API call this user would ever make – MrCaspan Oct 24 '21 at 22:33

0 Answers0