2
votes

Why do I get 429 error telling me that user-rate limit exceeded when I use only one request?:

$messagesResponse = $service->users_messages->listUsersMessages('me');

I got this error recently, I've checked my quotas, and as I got it listUserMessages (messages.list request) counts as 5 units, and I got 250 limit in a second.

Really can't figure it out.

Some code (here I also use search query, but it doesn't matter if I have the "q" param or not"):

class MessageReceiver implements ReceiverContract {

/**
 * @var Google_Client
 */
protected $client;

/**
 * Inject dependencies.
 */
public function __construct()
{
    $this->client = new Google_Client();
    $this->client->setClientId(config('services.gmail.clientId'));
    $this->client->setClientSecret(config('services.gmail.clientSecret'));
}

public function searchMessages($user, $query)
{
    $this->client->refreshToken($user->google_refresh_token);

    $service = new Google_Service_Gmail($this->client);
    $pageToken = null;
    $messages = [];

    do {
        $this->client->setUseBatch(false);
        try {
            $messagesResponse = $service->users_messages->listUsersMessages('me',
                ['pageToken' => $pageToken, 'q' => $query]
            );
        } catch (\Google_Service_Exception $e) {
            dd($e->getErrors()); // 429 error
        }
        ...
}
1
Can you please post full response (HTTP headers + body)?Eric D
How can I get http response when using php lib? listUserMessages throws an exception, and all I can do is to output some methods like getErrors.Victor

1 Answers

1
votes

Error 429, 'Too Many Requests'happens when your app is sending many requests at the same time. Maybe try implementing an exponential backoff?

https://developers.google.com/drive/web/handle-errors

Google's PHP client API is still in Beta so have a look at the Java implementation of it for now: https://developers.google.com/api-client-library/java/google-http-java-client/backoff