将电子邮件添加到 google 驱动器 returns 内部错误

Adding email to google drive returns Internal error

我一直在阅读 Google API,我了解到我可以做两个 POST 请求,首先我生成一个 Bearer 令牌,稍后我可以做一个 POST 请求到将电子邮件地址添加到 Google 驱动器文件夹中的给定文件夹。

我创建了:

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)

哪个returns:

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

按照指南,我似乎做对了,但仍然是 returns 内部错误。我想知道我做错了什么?

你做了什么检查数据?错误范围太广,其中一些只需要重试请求即可解决错误。

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.

使用指数退避时,请考虑以下事项:

  • 在错误发生后至少一秒开始重试。
  • 如果尝试的请求引入了更改,例如创建请求,请添加检查以确保没有重复的内容。重试请求无法解决一些错误,例如无效的授权凭据或“找不到文件”错误。

注:

  • 同时尝试测试变量的有效性。尝试使用有效的电子邮件地址进行检查。对于 ID 等无效变量,也会出现类似的问题。参见 related answer

资源: