Google Drive API quota exceeded

2019-02-27 20:46发布

问题:

I'm creating a channel that receive changes on users that are on my application. The main problem is that after 2-3 webhooks, I receive an error that says that user has exceeded the quota limits.

That has no sense, because I only received 2 post message (I saw it on ngrok).

I've went on google console on drive API and quota. Each time I receive a webhook the amount of queries is increased by 500. So, when a user make two changes and I receive two webhooks, the number of queries exceed the 1000 allowed by google and I receive that error.

That's the code where I enable the channel:

    @GET
    @Path("/enable")
    public void enable(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException {
        Credential credential = initFlow().loadCredential("user");
        Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
        Channel channel = new Channel(); 
        channel.setId(UUID.randomUUID().toString());
        channel.setType("web_hook");
        channel.setAddress("https://389825dc.ngrok.io/GDriveRest/app/gdrive/webhook");
        StartPageToken page = service.changes().getStartPageToken().execute();
        GDrive.savedPageToken = page.getStartPageToken();
        service.changes().watch(savedPageToken, channel).execute();
    }

And the following one is the webhook:

    @POST
    @Path("/webhook")
    public void webhook(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException {
        Credential credential = initFlow().loadCredential("user");
        Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
        String pageToken = savedPageToken;

        while (pageToken != null) {
            ChangeList changes = service.changes().list(pageToken).execute();

            for (Change change : changes.getChanges()) {
                Log.info("Change found for file: " + change.getFileId());
            }
            if (changes.getNewStartPageToken() != null) {
                savedPageToken = changes.getNewStartPageToken();
            }
            pageToken = changes.getNewStartPageToken();
        }
        response.setStatus(200);
    }

This is the error:

Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "usageLimits",
    "message" : "User Rate Limit Exceeded",
    "reason" : "userRateLimitExceeded"
  } ],
  "message" : "User Rate Limit Exceeded"
}

Why this is happening?

回答1:

There was an error in the code. I had to change the following:

PageToken = changes.getNewStartPageToken();

to

pageToken = changes.getNextPageToken();

The huge amount of queries was because I was requesting on every loop a new start page token, forcing an infinite loop.



回答2:

The error you have encountered was discussed in this guide.

Suggested actions:

  • Raise the per-user quota in the Developer Console project.
  • If one user is making a lot of requests on behalf of many users of a G Suite domain, consider a Service Account with authority delegation (setting the quotaUser parameter).
  • Use exponential backoff.

You can also try visiting this SO post for related issue.