我不明白为什么下面的代码可以在一个平台(Windows)上运行,而在另一个平台(Raspbian Linux,一个Raspberry Pi版本的Debian)上崩溃。下面的代码通过Google API身份验证:
import oauth2client.client
import httplib2
import platform
import apiclient
print(platform.python_version())
print((oauth2client.__version__, httplib2.__version__, apiclient.__version__))
with open('galarmclock.p12', 'rb') as f:
private_key = f.read()
credentials = oauth2client.client.SignedJwtAssertionCredentials(
'mail-from-api-console@developer.gserviceaccount.com',
private_key,
'https://www.googleapis.com/auth/calendar.readonly',
sub='a-sub@example.com'
)
http_auth = credentials.authorize(httplib2.Http())
service = apiclient.discovery.build('calendar', 'v3', http=http_auth)
print(service)
在Windows 8.1上,输出如预期:
3.4.3
('1.5.1', '0.9.2', '1.4.2')
<googleapiclient.discovery.Resource object at 0x0380F530>
在RPi上运行的相同代码在apiclient.discovery.build
调用时崩溃。
3.4.3+
('1.5.1', '0.9.2', '1.4.2')
Traceback (most recent call last):
File "testauthgoogle.py", line 19, in <module>
service = apiclient.discovery.build('calendar', 'v3', http=http_auth)
File "/usr/local/lib/python3.4/dist-packages/oauth2client/util.py", line 142, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/googleapiclient/discovery.py", line 196, in build
cache)
File "/usr/local/lib/python3.4/dist-packages/googleapiclient/discovery.py", line 242, in _retrieve_discovery_doc
resp, content = http.request(actual_url)
File "/usr/local/lib/python3.4/dist-packages/oauth2client/client.py", line 565, in new_request
self._refresh(request_orig)
File "/usr/local/lib/python3.4/dist-packages/oauth2client/client.py", line 835, in _refresh
self._do_refresh_request(http_request)
File "/usr/local/lib/python3.4/dist-packages/oauth2client/client.py", line 862, in _do_refresh_request
body = self._generate_refresh_request_body()
File "/usr/local/lib/python3.4/dist-packages/oauth2client/client.py", line 1541, in _generate_refresh_request_body
assertion = self._generate_assertion()
File "/usr/local/lib/python3.4/dist-packages/oauth2client/client.py", line 1670, in _generate_assertion
private_key, self.private_key_password), payload)
File "/usr/local/lib/python3.4/dist-packages/oauth2client/_openssl_crypt.py", line 120, in from_string
pkey = crypto.load_pkcs12(key, password).get_privatekey()
TypeError: must be str, not bytes
我不知道该往哪里走:Python和库版本是相同的,但是它们的行为不同。
问题是OpenSSL
在Rpi上的0.13
版本上。在Windows上是0.15
。
更新OpenSSL
修复了这个问题,两个代码运行相同。