【发布时间】:2014-05-29 05:12:04
【问题描述】:
我编写了一个 python 脚本来检查 GCS 中的文件,它使用 wxpython 来生成 GUI。为了进行身份验证,我以这种方式进行了验证(按照 Google 示例代码中的方式 -> http://code.google.com/p/google-cloud-platform-samples/source/browse/file-transfer-json/chunked_transfer.py?repo=storage):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
# the secrets file I put in same folder
scope=scope,
message=MISSING_CLIENT_SECRETS_MESSAGE)
credential_storage = CredentialStorage(CREDENTIALS_FILE) # the file to store
# authentication credentials
credentials = credential_storage.get()
if credentials is None or credentials.invalid:
credentials = run_oauth2(flow, credential_storage)
self.printLog('Constructing Google Cloud Storage service...')
http = credentials.authorize(httplib2.Http())
return discovery_build('storage', 'v1beta1', http=http)
上面的代码包含在我的Python脚本中,当它只是一个python .py文件时效果很好,后来我在win 7 64bit下使用pyinstaller将它转换为.exe(我也把秘密文件放在同一个文件夹作为 .exe 文件)使用以下命令
C:\gcs_file_check>python pyinstaller-2.0\pyinstaller.py -w gcs_file_check.py
点击 exe 时,Google 的 Request for Permission 页面 已正确启动(就像运行 Python 脚本而不是 exe 一样),但是在我点击接受后,上面的代码会抛出例外:
[Errno 185090050] _ssl.c:343: 错误:0B084002:x509 证书 例程:X509_load_cert_crl_file:系统库
我可以看到文件credentials.json不是由.exe创建的,而Python脚本可以正确创建这个文件。
有人能知道这是怎么发生的以及如何解决吗?感谢每一个答案!
====================
于 04/16 更新:
我添加了调试代码,发现异常正是来自下面的代码:
if credentials is None or credentials.invalid:
credentials = run_oauth2(flow, credential_storage)
====================
更新:
添加更多细节,之前我使用的是 oauth2client.tools.run()
from oauth2client.tools import run as run_oauth2
现在我按照建议的源代码更改为 run_flow() -> https://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.tools-pysrc.html#run
from oauth2client.tools import run_flow as run_oauth2
现在这部分代码是:
parser=argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
parents=[tools.argparser] )
flags = tools.argparser.parse_args(sys.argv[1:])
if credentials is None or credentials.invalid:
credentials = run_oauth2(flow, credential_storage, flags)
但还是,python代码运行良好,通过PyInstaller打包成.exe后抛出同样的异常[Errno 185090050]。
【问题讨论】:
-
不确定这是否是根本原因,但是从 EXE 打包程序内部运行的脚本通常具有与在包外部运行的相同脚本不同的“当前目录”设置。我建议在该代码之前打印 os.getcwd()。
-
感谢您的提示!我添加了代码 os.getcwd() 但没有看到任何奇怪的东西。后来我添加了调试代码,发现异常来自代码:if credentials is None or credentials.invalid: credentials = run_oauth2(flow, credential_storage),在我的问题中更新。
标签: python google-cloud-storage pyinstaller google-api-python-client