环境信息
- 操作系统:Ubuntu 16
- Python 版本:3.6(venv 内)
- gspread 版本:3.10
重现步骤
- 使用
Authlib
而不是oauth2client
withgspread
。 - 使用这里提到的代码。
- 尝试使用
gc.open
.
这是完整的回溯:
[Tue Sep 17 16:22:18.385805 2019] [wsgi:error] Traceback (most recent call last):
[Tue Sep 17 16:22:18.385811 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
[Tue Sep 17 16:22:18.385816 2019] [wsgi:error] response = self.full_dispatch_request()
[Tue Sep 17 16:22:18.385821 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
[Tue Sep 17 16:22:18.385827 2019] [wsgi:error] rv = self.handle_user_exception(e)
[Tue Sep 17 16:22:18.385847 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
[Tue Sep 17 16:22:18.385852 2019] [wsgi:error] reraise(exc_type, exc_value, tb)
[Tue Sep 17 16:22:18.385857 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
[Tue Sep 17 16:22:18.385861 2019] [wsgi:error] rv = self.dispatch_request()
[Tue Sep 17 16:22:18.385866 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
[Tue Sep 17 16:22:18.385871 2019] [wsgi:error] return self.view_functions[rule.endpoint](**req.view_args)
[Tue Sep 17 16:22:18.385875 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/myapp/views/event.py", line 64, in validate
[Tue Sep 17 16:22:18.385880 2019] [wsgi:error] worksheet = gc.open("Event Validation Test").sheet1
[Tue Sep 17 16:22:18.385884 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/gspread/client.py", line 123, in open
[Tue Sep 17 16:22:18.385889 2019] [wsgi:error] self.list_spreadsheet_files()
[Tue Sep 17 16:22:18.385893 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/gspread/client.py", line 96, in list_spreadsheet_files
[Tue Sep 17 16:22:18.385898 2019] [wsgi:error] res = self.request('get', url, params=params).json()
[Tue Sep 17 16:22:18.385902 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/gspread/client.py", line 73, in request
[Tue Sep 17 16:22:18.385907 2019] [wsgi:error] headers=headers
[Tue Sep 17 16:22:18.385911 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/requests/sessions.py", line 546, in get
[Tue Sep 17 16:22:18.385916 2019] [wsgi:error] return self.request('GET', url, **kwargs)
[Tue Sep 17 16:22:18.385920 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/authlib/client/assertion_session.py", line 43, in request
[Tue Sep 17 16:22:18.385925 2019] [wsgi:error] method, url, headers=headers, data=data, auth=auth, **kwargs)
[Tue Sep 17 16:22:18.385930 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/requests/sessions.py", line 519, in request
[Tue Sep 17 16:22:18.385934 2019] [wsgi:error] prep = self.prepare_request(req)
[Tue Sep 17 16:22:18.385939 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/requests/sessions.py", line 462, in prepare_request
[Tue Sep 17 16:22:18.385944 2019] [wsgi:error] hooks=merge_hooks(request.hooks, self.hooks),
[Tue Sep 17 16:22:18.385949 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/requests/models.py", line 317, in prepare
[Tue Sep 17 16:22:18.385954 2019] [wsgi:error] self.prepare_auth(auth, url)
[Tue Sep 17 16:22:18.385966 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/requests/models.py", line 548, in prepare_auth
[Tue Sep 17 16:22:18.385971 2019] [wsgi:error] r = auth(self)
[Tue Sep 17 16:22:18.385975 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/authlib/client/assertion_session.py", line 9, in __call__
[Tue Sep 17 16:22:18.385980 2019] [wsgi:error] self.ensure_refresh_token()
[Tue Sep 17 16:22:18.385984 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/authlib/client/assertion_client.py", line 9, in ensure_refresh_token
[Tue Sep 17 16:22:18.385989 2019] [wsgi:error] return self.client.refresh_token()
[Tue Sep 17 16:22:18.385993 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/authlib/oauth2/rfc7521/client.py", line 55, in refresh_token
[Tue Sep 17 16:22:18.385998 2019] [wsgi:error] **self._kwargs
[Tue Sep 17 16:22:18.386002 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/authlib/oauth2/rfc7523/grant.py", line 24, in sign
[Tue Sep 17 16:22:18.386007 2019] [wsgi:error] expires_at, claims, **kwargs)
[Tue Sep 17 16:22:18.386012 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/authlib/oauth2/rfc7523/assertion.py", line 36, in sign_jwt_bearer_assertion
[Tue Sep 17 16:22:18.386016 2019] [wsgi:error] return jwt.encode(header, payload, key)
[Tue Sep 17 16:22:18.386021 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/authlib/jose/rfc7519/jwt.py", line 95, in encode
[Tue Sep 17 16:22:18.386025 2019] [wsgi:error] return self._jws.serialize_compact(header, text, key)
[Tue Sep 17 16:22:18.386030 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/authlib/jose/rfc7515/jws.py", line 71, in serialize_compact
[Tue Sep 17 16:22:18.386035 2019] [wsgi:error] self._algorithms, jws_header, payload, key, private=True)
[Tue Sep 17 16:22:18.386039 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/authlib/jose/util.py", line 12, in prepare_algorithm_key
[Tue Sep 17 16:22:18.386044 2019] [wsgi:error] key = algorithm.prepare_private_key(key)
[Tue Sep 17 16:22:18.386048 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/authlib/jose/rfc7518/_backends/_key_cryptography.py", line 19, in prepare_private_key
[Tue Sep 17 16:22:18.386053 2019] [wsgi:error] return load_pem_private_key(key, password=None, backend=default_backend())
[Tue Sep 17 16:22:18.386058 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/cryptography/hazmat/backends/__init__.py", line 15, in default_backend
[Tue Sep 17 16:22:18.386063 2019] [wsgi:error] from cryptography.hazmat.backends.openssl.backend import backend
[Tue Sep 17 16:22:18.386067 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/__init__.py", line 7, in <module>
[Tue Sep 17 16:22:18.386076 2019] [wsgi:error] from cryptography.hazmat.backends.openssl.backend import backend
[Tue Sep 17 16:22:18.386081 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 18, in <module>
[Tue Sep 17 16:22:18.386085 2019] [wsgi:error] from cryptography import utils, x509
[Tue Sep 17 16:22:18.386090 2019] [wsgi:error] File "/var/www/abc.example.com/public_html/venv/lib/python3.6/site-packages/cryptography/x509/__init__.py", line 7, in <module>
[Tue Sep 17 16:22:18.386095 2019] [wsgi:error] from cryptography.x509 import certificate_transparency
[Tue Sep 17 16:22:18.386099 2019] [wsgi:error] ImportError: cannot import name certificate_transparency
到目前为止我已经尝试过:
- 检查文件路径是否正确。
certificate_transparency.py
确实存在于/venv/lib/python3.6/site-packages/cryptography/x509
. - 在 Windows 上尝试了相同的方法,它可以正常工作。
- 重新安装所有依赖项,包括
pyOpenSSL
. - 尝试在
(venv)
Python 控制台中导入它。>>> from cryptography.x509 import certificate_transparency
工作正常。
我的代码:
def create_assertion_session(conf_file, scopes, subject=None):
with open(conf_file, 'r') as f:
conf = json.load(f)
token_url = conf['token_uri']
issuer = conf['client_email']
key = conf['private_key']
key_id = conf.get('private_key_id')
header = {'alg': 'RS256'}
if key_id:
header['kid'] = key_id
# Google puts scope in payload
claims = {'scope': ' '.join(scopes)}
return AssertionSession(
grant_type=AssertionSession.JWT_BEARER_GRANT_TYPE,
token_url=token_url,
issuer=issuer,
audience=token_url,
claims=claims,
subject=subject,
key=key,
header=header,
)
scopes = [
'https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive',
]
session = create_assertion_session(json_key_file, scopes)
gc = Client(None, session)
worksheet = gc.open("Event Validation Test").sheet1 # this causes the error