使用Gmail API时出现BrokenPipeError

时间:2017-12-29 07:38:37

标签: python pipe gmail-api google-apis-explorer

我使用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

这里的问题是什么?

由于

1 个答案:

答案 0 :(得分:0)

似乎在_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