【发布时间】:2023-03-13 16:10:01
【问题描述】:
此代码在 Python2 中完美运行,但在 Python3 中却不行。我经历了很多可能的解决方案,但没有一个。他们似乎工作。我怎样才能做到这一点。在 python3 中工作?
错误日志:
[Tue Jul 10 07:23:21.713813 2018] [wsgi:error] [pid 1667:tid 140651010107136]
h = httplib2.Http()
[Tue Jul 10 07:23:21.713843 2018] [wsgi:error] [pid 1667:tid 140651010107136]
File "/usr/lib/python3.5/json/__init__.py", line 312, in loads
[Tue Jul 10 07:23:21.713874 2018] [wsgi:error] [pid 1667:tid 140651010107136]
s.__class__.__name__))
[Tue Jul 10 07:23:21.713907 2018] [wsgi:error] [pid 1667:tid 140651010107136]
TypeError: the JSON object must be str, not 'bytes'
代码:
# Check that the access token is valid.
access_token = credentials.access_token
url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s'
% access_token)
h = httplib2.Http()
result = json.loads(h.request(url, 'GET')[1])
# If there was an error in the access token info, abort.
if result.get('error') is not None:
response = make_response(json.dumps(result.get('error')), 500)
response.headers['Content-Type'] = 'application/json'
return response
【问题讨论】:
标签: python json python-3.x python-2.x httplib2