1
votes

The Google Drive API keeps throwing 500 errors at me and I have run out of ideas.

The returned pages for the 502 error reads:

That’s an error. The server encountered a temporary error and could not complete your request. 
Please try again in 30 seconds.        
That’s all we know.

Sometimes I also get a 500 whose error message basically says "no idea" and does not even suggest retry.

I made a toy example to reproduce the problem:

import gspread
import random
import json
from oauth2client.client import SignedJwtAssertionCredentials

def main():

    json_key = json.load(open("/home/me/.credentials/API Project-56725514b81f.json"))
    scope = ['https://spreadsheets.google.com/feeds']

    credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
    client = gspread.authorize(credentials)

    #prior to run, create a spreadsheet "Test" with the sheets "Sheet 1", "Sheet 2", "Sheet 3"
    filename =  "Test"
    spreadsheet = client.open(filename)
    while True:
        sheet='Sheet %i' %random.choice([1,2,3])
        wks = spreadsheet.worksheet(sheet)
        range= wks.range('A1:Z1')
        range[0].value = random.random()
        range[1].value = random.random()
        range[2].value = random.random()
        wks.update_cells(range)

if __name__ == '__main__':

    main()

I tested the following hypothesis:

  • The allowed number of requests is exceeded -> I checked the Developer Console for my oauth user -> 10.000.000 requests per day are allowed, the user limit is also set high
  • The error message indicated that waiting could help. I implemented exponential backoff as suggested in the Api Docs, but I get a "ResponseNotReady" exception from gspread no matter how long I wait until retry.

More information

credentials object except "private_key" and "service_account_name":

{"id_token": null, "token_uri": "https://accounts.google.com/o/oauth2/token", "token_response": null, "client_id": null, "scope": "https://spreadsheets.google.com/feeds", "token_expiry": null, "_class": "SignedJwtAssertionCredentials", "refresh_token": null, "_module": "oauth2client.client", "private_key_password": "notasecret", "access_token": null, "invalid": false, "assertion_type": null, "kwargs": {}, "client_secret": null, "revoke_uri": "https://accounts.google.com/o/oauth2/revoke", "store": null, "user_agent": null}

1

1 Answers

1
votes

I'm not sure which library you're using, but the line client = gspread.login([email protected], password) implies it's an out of date one. There is no longer any way to authenticate to Google APIs using username and password.

Depending on your programming language, you should either find a more up to date library, or program against the REST API directly.