使用Gmail API时出现BrokenPipeError

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Gmail API时出现BrokenPipeError相关的知识,希望对你有一定的参考价值。

我正在使用Gmail API在Python 3中发送带附件的电子邮件。我正在尝试使用与Google开发人员相同的代码,如下所示:

https://developers.google.com/gmail/api/guides/sending

问题是当附件是4.2KB或2.6MB时,代码运行良好;但是当附件为3.0MB或9.6MB或更大时,会出现错误:

Traceback (most recent call last):
  File "quickstart2.py", line 184, in <module>
    main()
  File "quickstart2.py", line 170, in main
    send_message(service, "me", message)
  File "quickstart2.py", line 147, in send_message
    message = (service.users().messages().send(userId=user_id, body=message).execute())
  File "/home/yizhu/anaconda3/lib/python3.6/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/yizhu/anaconda3/lib/python3.6/site-packages/googleapiclient/http.py", line 837, in execute
    method=str(self.method), body=self.body, headers=self.headers)
  File "/home/yizhu/anaconda3/lib/python3.6/site-packages/googleapiclient/http.py", line 176, in _retry_request
    raise exception
  File "/home/yizhu/anaconda3/lib/python3.6/site-packages/googleapiclient/http.py", line 163, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "/home/yizhu/anaconda3/lib/python3.6/site-packages/oauth2client/transport.py", line 175, in new_request
    redirections, connection_type)
  File "/home/yizhu/anaconda3/lib/python3.6/site-packages/oauth2client/transport.py", line 282, in request
    connection_type=connection_type)
  File "/home/yizhu/anaconda3/lib/python3.6/site-packages/httplib2/__init__.py", line 1322, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/home/yizhu/anaconda3/lib/python3.6/site-packages/httplib2/__init__.py", line 1072, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/home/yizhu/anaconda3/lib/python3.6/site-packages/httplib2/__init__.py", line 996, in _conn_request
    conn.request(method, request_uri, body, headers)
  File "/home/yizhu/anaconda3/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/home/yizhu/anaconda3/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/home/yizhu/anaconda3/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/home/yizhu/anaconda3/lib/python3.6/http/client.py", line 1065, in _send_output
    self.send(chunk)
  File "/home/yizhu/anaconda3/lib/python3.6/http/client.py", line 986, in send
    self.sock.sendall(data)
  File "/home/yizhu/anaconda3/lib/python3.6/ssl.py", line 972, in sendall
    v = self.send(byte_view[count:])
  File "/home/yizhu/anaconda3/lib/python3.6/ssl.py", line 941, in send
    return self._sslobj.write(data)
  File "/home/yizhu/anaconda3/lib/python3.6/ssl.py", line 642, in write
    return self._sslobj.write(data)
BrokenPipeError: [Errno 32] Broken pipe

这有什么问题?

谢谢

答案

似乎在_retry_request中引发了异常。我自己没有遇到过这个错误,但github上有一个关于同一错误的讨论。 https://github.com/google/google-api-python-client/issues/218

尝试使用httplib2shim,似乎oauth2client仍未被google-auth取代。

我发现的另一个建议是对超过10 MB的文件使用MEDIA / upload选项。有关如何使用/上传的文档:https://developers.google.com/gmail/api/v1/reference/users/messages/send

以上是关于使用Gmail API时出现BrokenPipeError的主要内容,如果未能解决你的问题,请参考以下文章

尝试通过 API 删除 gmail 邮件时出现 401 错误

访问 Gmail API 时出现错误请求

使用 Gmail api 时尝试获取电子邮件列表时出现错误 [403]

使用 Gmail API 发送大型附件时出现错误 10053

发布到 Gmail API 时出现 http 500 后端错误

在 C# 中调用 Gmail 服务 API 时出现 System.Net.Http.HttpRequestException