【发布时间】:2017-08-04 21:13:47
【问题描述】:
使用从 Google 控制台下载的 Google API JSON 密钥文件,并使用 google-api-python-client==1.5.5 和 oauth2client==4.0.0,并在我的本地 Mac 上使用 python 3.5,这可以正常工作:
from oauth2client.service_account import ServiceAccountCredentials
scopes = ['https://www.googleapis.com/auth/calendar']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
settings.GOOGLE_KEY_FILE_PATH, scopes).create_delegated(
settings.GOOGLE_SUB_USER)
我可以继续查询 API。但是在生产 Ubuntu 服务器上(在 Python 3.4 上),使用相同的代码和密钥文件,我得到:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/.../python3.4/site-packages/oauth2client/service_account.py", line 220, in from_json_keyfile_name
client_credentials = json.load(file_obj)
File "/usr/lib/python3.4/json/__init__.py", line 265, in load
return loads(fp.read(),
File "/.../python3.4/codecs.py", line 319, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 1: invalid start byte
依赖相同,代码相同,keyfile相同。 Python 3.4 和 3.5 之间是否有一些差异可以解释这一点?或者我可以做些什么来重新编码使 Python 3.4 满意的密钥文件?还是发生了其他事情?
【问题讨论】:
-
我已经回到使用 Google 提供的 .p12 密钥文件而不是 JSON 来解决这个问题。