python - Google GMail API `installed` 应用程序显示来自模块 SSL.py 的 IOError 13 w/o sudo

标签 python ssl privileges gmail-api

我正在尝试使用 GMAIL API 创建一个应用程序来从我的服务器发送自动电子邮件。以简单用户身份运行我的应用程序会产生以下结果:

pankgeorg@snf-25181:~/tomotech/gmailer$ python mailer.py
Traceback (most recent call last):
  File "mailer.py", line 36, in <module>
    gmail_service = build('gmail', 'v1', http=http)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client-1.4.5-py2.7.egg/oauth2client/util.py", line 135, in positional_wrapper
  File "/usr/local/lib/python2.7/dist-packages/google_api_python_client-1.3.1-py2.7.egg/googleapiclient/discovery.py", line 198, in build
  File "/usr/local/lib/python2.7/dist-packages/oauth2client-1.4.5-py2.7.egg/oauth2client/util.py", line 135, in positional_wrapper
  File "/usr/local/lib/python2.7/dist-packages/oauth2client-1.4.5-py2.7.egg/oauth2client/client.py", line 547, in new_request
  File "/usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg/httplib2/__init__.py", line 1593, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg/httplib2/__init__.py", line 1335, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg/httplib2/__init__.py", line 1257, in _conn_request
    conn.connect()
  File "/usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg/httplib2/__init__.py", line 1021, in connect
    self.disable_ssl_certificate_validation, self.ca_certs)
  File "/usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg/httplib2/__init__.py", line 80, in _ssl_wrap_socket
    cert_reqs=cert_reqs, ca_certs=ca_certs)
  File "/usr/lib/python2.7/ssl.py", line 886, in wrap_socket
    ciphers=ciphers)
  File "/usr/lib/python2.7/ssl.py", line 496, in __init__
    self._context.load_verify_locations(ca_certs)
IOError: [Errno 13] Permission denied

另一方面,使用 sudo 运行效果很好

pankgeorg@snf-25181:~/tomotech/gmailer$ sudo python mailer.py
Message Id: 14ad0aea05e*****

老实说,为了使用 --noauth_local_webserver 进行身份验证,我使用 sudo 运行命令,通过身份验证并 chown gmail.storage 再次给自己。 另外,我使用 easy_install 安装,因为 pip install 给我以下错误:

pankgeorg@snf-25181:~/tomotech/gmailer$ sudo pip install --upgrade google_api_python_client
Cleaning up...
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
  File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 278, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
  File "/usr/local/lib/python2.7/dist-packages/pip/req.py", line 1096, in prepare_files
    req_to_install, self.upgrade)
  File "/usr/local/lib/python2.7/dist-packages/pip/index.py", line 194, in find_requirement
    page = self._get_page(main_index_url, req)
  File "/usr/local/lib/python2.7/dist-packages/pip/index.py", line 568, in _get_page
    session=self.session,
  File "/usr/local/lib/python2.7/dist-packages/pip/index.py", line 670, in get_page
    resp = session.get(url, headers={"Accept": "text/html"})
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/sessions.py", line 395, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 237, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/sessions.py", line 383, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/sessions.py", line 506, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/sessions.py", line 168, in resolve_redirects
    allow_redirects=False,
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/sessions.py", line 486, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/adapters.py", line 330, in send
    timeout=timeout
  File "/usr/local/lib/python2.7/dist-    packages/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 480, in urlopen
    body=body, headers=headers)
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 285, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python2.7/httplib.py", line 1001, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 1035, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 997, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 850, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 826, in send
    self.sock.sendall(data)
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.py", line 323, in sendall
    return self.connection.sendall(data)
  File "/usr/lib/python2.7/dist-packages/OpenSSL/SSL.py", line 969, in sendall
    raise TypeError("buf must be a byte string")
TypeError: buf must be a byte string

Storing debug log for failure in /root/.pip/pip.logrom oath2client import tool

据我了解,这两种情况的问题根源是相同的。 我还必须注意我的笔记本电脑(我可以在其上正常进行身份验证,在网络浏览器上,而不是 --noauth_local_webserver 上)它工作得很好,即使安装是以相同的方式完成的(pip 的问题也出现在那里)。

提前致谢,抱歉发了这么长的帖子! 我使用的教程: parse arg code for sending mails 应用程序正文几乎是 gmail api 的快速入门。

最佳答案

httplib2 安装程序为其 httplib2/cacerts.txt 文件设置了不正确的权限。一种解决方案是通过运行简单地使其文件对任何人都可读

chmod o+r -R  /usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg

但是,最好卸载 pip 安装的版本,并改用操作系统的包管理器,它可能对所有文件都有正确的权限。在 Debian 上,这可以通过

pip uninstall httplib2
apt-get install python-httplib2

关于python - Google GMail API `installed` 应用程序显示来自模块 SSL.py 的 IOError 13 w/o sudo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27870024/

相关文章:

python - 如何在给定引理和标记的情况下反转词形还原过程?

python - 如何在没有客户端 SSL 证书的情况下在 python gRPC 客户端中打开安全通道

C# HttpWebRequest SEC_I_RENEGOTIATE 间歇性错误

c# - C#中的Process.Start()对非管理员用户不起作用

python appengine unicodeencodeerror on search api snippeted results

python - python中的下采样

docker - 在 docker 容器中使用 apache 服务器配置 SSL

postgresql - 授予所有用户当前和 future 的权限

mysql - 处理 phpMyAdmin 的用户

python - 如何在 Mercurial 中应用补丁并在应用失败时显示差异工具