Getting 500 Error when using Google Drive API to u

2019-04-21 06:07发布

问题:

I am getting 500 error when I am trying to give access permission of any file. I don't want to send e-mail when the file is shared with someone. I want to stop the notification emails.

  Insert insert = service.permissions().insert(fileId, newPerm);
  insert.setSendNotificationEmails(false);
  newPerm = insert.execute();


        500 OK
            {
            "code" : 500,
            "errors" : [ {
            "domain" : "global",
            "message" : "Internal Error. User message: \"An internal                                                     error has occurred which prevented the sharing of these item(s): fileame\"",
           "reason" : "internalError"
            } ],
          "message" : "Internal Error. User message: \"An internal error has occurred which prevented the sharing of these item(s): filename\""
            }

Appreciate any suggestion.

回答1:

I started getting this problem but managed to workaround it by introducing a pause between file push and permission insert.

my 'workflow' (using http posts rather than rest client):

  1. Push file.
  2. Get fileId out of response from Google Drive API.
  3. Send payload to insert permission using fileId

I was always getting the 500 'Internal error' at Step 3; the file was always being pushed successfully.

On my initial debugging, when stepping through, I'd try and push the same file to Google again, and it'd return the same fileId as on previous attempts with the same file (so Google Drive API must recongise the file is the same).

But when I attempted subsequent permission inserts (still debugging) with the same fileId, it was ALWAYS successful.

So I just introduced a 20s pause between file push and permission insert, which has done the trick (as time isn't critical in my application).

There must be some kind of replication delay (or 'time_til_permissions_can_be_inserted') in Google Drive. Hard to believe, seeing as it's Google, but hey.



回答2:

In another answer to this question, @eversMcc had the right idea: pause for a bit. The error is due to Google Drive being busy processing that file (creating it, pushing it to make it globally-accessible, or otherwise updating it). Once the flurry of activity settles down, then adding additional users to the document shouldn't be a problem.

You can see from this Drive API documentation page a description of the (500) error you received as well as the recommended course of action which is to implement exponential backoff, which is really just saying you should pause a bit before trying again & extending that delay each time you get the same error.

While @eversMcc implemented a 20s pause, you don't have to use a fixed backoff duration, hence why exponential is useful. Why wait 20s when only 4 or 8 suffice? Another resource is this descriptive blog post. If you don't want to implement it yourself, you can try to find something similar to these (Python-based) retrying or backoff packages. Check out this related SO Q&A if you want an example of rolling your own.

Use the Drive API for adding permissions or any file-related operations like sharing, copying, import/export, etc., but for any document-oriented operations, say with Sheets spreadsheets, you would be using the Google Sheets API instead.

NOTE 1: there was a similar issue a year before this post but it was fixed as per this SO Q&A.

NOTE 2: Drive API v2's permissions.insert() command changed names to permissions.create() in v3 -- see migration guide for more details



回答3:

This is most likely a transient error in the Drive API infrastructure. You may experience these kind of behaviors sometimes, caused due to a network or a server issue on Google's end.

Unfortunately, there is a very limited info on those kind of errors. A suggested strategy would be to perform and exponential backoff, as explained here and in more detail here.

In some (strange) cases, the issue may last longer than expected, which is a pain and can be frustrating for the developer, but it will eventually disappear, hopefully within 24-48 hours, and everything will be back to normal. If the error persists for a long time, you should request a trace token to a Drive API Support agent, which will generate further information internally that can be analyzed by Engineering.