0

I have been reading Google API and I have learned that I could do two POST requests where first I generate a Bearer token which I later on can do a POST request to add email adress to given folder in Google drive folder.

I have created:

import requests

access_token = requests.post(
    'https://www.googleapis.com/oauth2/v4/token',
    headers={'content-type': 'application/x-www-form-urlencoded'},
    data={
        'grant_type': 'refresh_token',
        'client_id': 'xxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
        'client_secret': '07Sxxxxxxxxxxxxxxxx1qpa',
        'refresh_token': '1//04xxxxxxxxxxxxxxxxxxxxxxxxxxxs',
    }
)

folder_id = "1_5akXx6xB8YighNhtwfI46SO0CI2hiuh"

headers = {
    'Authorization': f"Bearer {access_token.json()['access_token']}",
    'Accept': 'application/json',
    'Content-Type': 'application/json'
}

data = {
    "role": "reader",
    "type": "user",
    "emailAddress": "test@gmail.com"
}

response = requests.post(
    f'https://www.googleapis.com/drive/v3/files/{folder_id}/permissions',
    headers=headers,
    json=data
)

print(headers)
print(response.text)

Which returns:

{
     "error": {
      "errors": [
       {
        "domain": "global",
        "reason": "internalError",
        "message": "Internal Error"
       }
      ],
      "code": 500,
      "message": "Internal Error"
     }
    }

By following the guide it seems like I am doing it correct but still it returns Internal error. I wonder what am I doing wrong?

PythonNewbie
  • 1,031
  • 1
  • 15
  • 33

1 Answers1

1

What have you done to check the data? The error is too broad and some of them just need the request to be retried to resolve the error.

You can periodically retry a failed request over an increasing amount of time to handle errors related to rate limits, network volume, or response time. For example, you might retry a failed request after one second, then after two seconds, and then after four seconds. This method is called exponential backoff and it is used to improve bandwidth usage and maximize throughput of requests in concurrent environments.

When using exponential backoff, consider the following:

  • Start retry periods at least one second after the error.
  • If the attempted request introduces a change, such as a create request, add a check to make sure nothing is duplicated. Some errors, such as invalid authorization credentials or "file not found" errors, aren’t resolved by retrying the request.

Note:

  • Also try to test the validity of your variables. Try a valid emailAddress to check. Similar issues are showing for invalid variables such as IDs. See related answer

Resource:

NightEye
  • 10,634
  • 2
  • 5
  • 24
  • I think I might found it. If you enter invalid gmail that has never been registered. You will get the error I believe - Could that be a possbility? – PythonNewbie Jun 07 '21 at 22:25
  • Try a valid emailAddress to check. Similar issues are showing for invalid IDs. See [related answer](https://stackoverflow.com/a/26405387/16132436) – NightEye Jun 07 '21 at 22:32
  • 1
    Yupp, it was that :D When I tried my own and my friends gmail, worked. If I typed random mail like 44gg@gmail.com then it would throw the error – PythonNewbie Jun 07 '21 at 22:36
  • I neglected the email on the get go as I was in the assumption that you just censored your email for privacy purposes. I'm doing that too by default, `test@gmail.com` haha – NightEye Jun 07 '21 at 22:39
  • 1
    Yupp haha, oh well. It was atleast good that I found it after 3 hours :') but sad that Google doesnt print out better errors however.. – PythonNewbie Jun 07 '21 at 22:41
  • Yeah. everyone is already asking for a more descriptive error. Hopefully they can in the near future. As per the linked related answer, this should be error 400 – NightEye Jun 07 '21 at 22:43