Google API 和 cx_Freeze 无法正常工作

Posted

技术标签:

【中文标题】Google API 和 cx_Freeze 无法正常工作【英文标题】:Google API and cx_Freeze not working properly 【发布时间】:2013-05-19 12:32:07 【问题描述】:

我制作了一个能够将文件上传到 Google Drive 的 Python 程序。 .py 文件完美运行,不会出现任何错误。

但是由于我要为没有安装 Python 的人分发它,所以我必须将程序和资源转换为 .exe。我已经用 cx_Freeze 做到了这一点。我以前用过,一直有效。

但现在,运行 .exe 时,Google Drive API 似乎会导致错误。以下是它给出的错误:

Traceback (most recent call last):
File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
  "__main__", fname, loader, pkg_name)

File "C:\Python27\lib\runpy.py", line 72, in _run_code
  exec code in run_globals

File "Test.py", line 41, in <module>

File "C:\Python27\lib\oauth2client\util.py", line 128, in positional_wrapper
  return wrapped(*args, **kwargs)

File "C:\Python27\lib\oauth2client\client.py", line 1283, in step2_exchange
  headers=headers)

File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1570, in request
  (response, content) = self._request(conn, authority, uri, request_uri, metho
d, body, headers, redirections, cachekey)

File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1317, in _request
  (response, content) = self._conn_request(conn, request_uri, method, body, he
aders)

File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1252, in _conn_request
  conn.connect()

File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1021, in connect
  self.disable_ssl_certificate_validation, self.ca_certs)

File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 80, in _ssl_wrap_socket
  cert_reqs=cert_reqs, ca_certs=ca_certs)

File "C:\Python27\lib\ssl.py", line 383, in wrap_socket
  ciphers=ciphers)

File "C:\Python27\lib\ssl.py", line 141, in __init__
  ciphers)

ssl.SSLError: [Errno 185090050] _ssl.c:340: error:0B084002:x509 certificate rout
ines:X509_load_cert_crl_file:system lib

用户输入授权码后出现此错误。

经过一些调试,我发现导致这个问题的行是

credentials = flow.step2_exchange(code)

这是一个sn-p的代码:

flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
webbrowser.open(authorize_url)
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)

CLIENT_ID、CLIENT_SECRET、OAUTH_SCOPE 和 REDIRECT_URI 均已正确配置。

那么,这里有什么问题呢?

如果您需要查看更多代码,请提出,我会更新此问题。

【问题讨论】:

【参考方案1】:

似乎找不到您的 SSL 证书。这是可以理解的,因为 httplib2 从文件系统加载它们,并没有告诉 cx_Freeze 将它们放入捆绑包中。

阅读本文,对您有帮助:https://github.com/kennethreitz/requests/issues/557#issuecomment-6420819

【讨论】:

好的,现在可以了,我只需要在我的http = httplib2.Http() 中添加cacerts 的路径,将其移动到credentials = flow.step2_exchange(code) 之前执行并将其更改为credentials = flow.step2_exchange(code, http=http) 您可以像这样指示 cx_Freeze 包含 cacerts.txt 文件:"include_files": [(httplib2.CA_CERTS, 'cacerts.txt')],但请注意,这不会将其存储在httplib2 期望的地方(你不能,因为 cx_Freeze 压缩了那个文件夹),所以你需要遵循上面 NotTheOP 的建议。

以上是关于Google API 和 cx_Freeze 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 virtualenv 和 cx_Freeze 将 pygame 转换为可执行文件

无法使用 cx_freeze 和 PySide2 进行编译

cx_Freeze- ImportError:无法导入名称设置

cx_freeze 无法构建 numpy 可执行文件

无法为 Python 3.7 64 位安装 cx_Freeze 或 scipy

使用 python 3.7 PyQt5 和 cx_Freeze 创建可执行文件,但 DLL 无法加载