Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将电子邮件添加到google drive返回内部错误_Python_Python 3.x_Python Requests_Google Drive Api - Fatal编程技术网

Python 将电子邮件添加到google drive返回内部错误

Python 将电子邮件添加到google drive返回内部错误,python,python-3.x,python-requests,google-drive-api,Python,Python 3.x,Python Requests,Google Drive Api,我一直在阅读,我了解到我可以做两个POST请求,首先我生成一个承载令牌,然后我可以做一个POST请求,将电子邮件地址添加到Google drive文件夹中的给定文件夹中 我创造了: import requests access_token = requests.post( 'https://www.googleapis.com/oauth2/v4/token', headers={'content-type': 'application/x-www-form-urlencode

我一直在阅读,我了解到我可以做两个POST请求,首先我生成一个承载令牌,然后我可以做一个POST请求,将电子邮件地址添加到Google drive文件夹中的给定文件夹中

我创造了:

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)
返回:

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

通过遵循指南,看起来我做的是正确的,但它仍然返回内部错误。我想知道我做错了什么?

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

您可以在不断增加的时间内定期重试失败的请求,以处理与速率限制、网络容量或响应时间相关的错误。例如,您可能会在一秒钟、两秒钟和四秒钟后重试失败的请求。这种方法称为指数退避,用于在并发环境中提高带宽利用率和最大化请求吞吐量

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

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

  • 还要试着测试变量的有效性。请尝试检查有效的电子邮件地址。对于无效变量(如ID),也会出现类似问题。看
资源:

我想我可能找到了。如果您输入了从未注册过的无效gmail。我相信您会得到错误-这可能吗?请尝试使用有效的电子邮件地址进行检查。无效ID也会出现类似问题。看到了,是这样的:当我尝试我自己和我朋友的gmail时,D成功了。如果我输入随机邮件,就像44gg@gmail.com然后它会抛出错误我一开始就忽略了电子邮件,因为我假设你只是出于隐私目的审查了你的电子邮件。默认情况下,我也在这样做,
test@gmail.com
hahaYupp哈哈,哦,好吧。我在3个小时后发现了它,这至少是件好事:),但遗憾的是谷歌没有打印出更好的错误。。