gimite / google-drive-ruby

A Ruby library to read/write files/spreadsheets in Google Drive/Docs.
Other
1.76k stars 491 forks source link

Google::Apis::ClientError: Invalid request status_code: 403 #368

Open carlosver17 opened 4 years ago

carlosver17 commented 4 years ago

Hello ! My name is Carlos and I'm writting from Chile.

I'm using google-drive-ruby gem and most of the time works fine but I have a problem in some occasions when I try to write on a spreedsheet I'm getting from google a status 403, "Fobidden" with the following message: "We're sorry... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now.

Here it's a part og the log on production:

[2020-03-30T04:55:48.904982 #28790] DEBUG -- : Sending HTTP get https://www.googleapis.com/drive/v3/files?fields=%2A&pageSize=1&q=%28mimeType%20%3D%20%27application%2Fvnd.google-apps.spreadsheet%27%29%20and%20%28name%20%3D%20%27Libro%20novedades%27%29&supportsTeamDrives=true

[2020-03-30T04:55:57.029218 #28790] DEBUG -- : #<HTTP::Message:0x00007fc2a98167d0 @http_header=#<HTTP::Message::Headers:0x00007fc2a98167a8 @http_version="1.1", @body_size=0, @chunked=false, @request_method="GET", @request_uri=#<Addressable::URI:0x3fe154c0b884 URI:https://www.googleapis.com/drive/v3/files?fields=%2A&pageSize=1&q=%28mimeType%20%3D%20%27application%2Fvnd.google-apps.spreadsheet%27%29%20and%20%28name%20%3D%20%27Libro%20novedades%27%29&supportsTeamDrives=true>, @request_query=nil, @request_absolute_uri=nil, @status_code=403, @reason_phrase="Forbidden", @body_type=nil, @body_charset=nil, @body_date=nil, @body_encoding=#, @is_request=false

[2020-03-30T04:55:57.029471 #28790] DEBUG -- : Caught error Invalid request D, [2020-03-30T04:55:57.029721 #28790] DEBUG -- : Error - #<Google::Apis::ClientError: Invalid request status_code: 403

I checked the quotas and they are not exceeded.

Normally, if I reload the page it works again.

Any ideas?

Regards!

gimite commented 4 years ago

Sorry for a slow response.

Just guessing from the error message, but you may be hitting the API too frequently. If you make multiple requests simultaneously, how about putting some sleep between requests?

You can also catch the error and retry (again, maybe after some sleep) if reload helps. The API is known to fail occasionally, so retry helps in general.

carlosver17 commented 4 years ago

Hello Hiroshi,

Thank you for your reply!

Actually I’m not making multiple requests simultaneously. Normally is one every 15 or 30 minutes.

This is the code from the controller (Text deleted):

` require 'bundler' Bundler.require

session = GoogleDrive::Session.from_service_account_key("Deleted")

spreadsheet = session.spreadsheet_by_title("Deleted")

worksheet = spreadsheet.worksheets.first

worksheet.set_text_format( worksheet.num_rows + 1, 1, 1, 4, bold: false, font_size: 14, font_family: "Arial", foreground_color: nil )

worksheet.insert_rows(worksheet.num_rows + 1, [ ["Text2", "Text2", "Text3", "Text4"] ])

worksheet.save redirect_to root_path `

Any ideas?

Regards,

gimite commented 4 years ago

Assuming that your service is a web service, isn't it still possible that the service sometimes get high traffic, resulting in high frequency requests to the API?

I would still suggest to retry automatically on failures anyways.

The error message is a bit weird because Google should assume that their APIs receive "automated queries" (since it's an API). It may be worth filing a bug report to Google.

carlosver17 commented 4 years ago

I'll try!

Thank you!