不确定我在这里做错了什么。我在macbook pro上运行python 3.5。鉴于这本词典:
data = {'request': {'passengers': {'adultCount': 2,
'childCount': 0,
'infantInLapCount': 0,
'infantInSeatCount': 0,
'seniorCount': 0},
'refundable': 'false',
'slice': [{'date': '2016-01-01',
'destination': 'PMI',
'maxStops': '0',
'origin': 'ZRH'},
{'date': '2016-01-10',
'destination': 'ZRH',
'maxStops': 0,
'origin': 'PMI'}],
'solutions': 50}}
所以应引用引用的所有内容。
现在我做了:
url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key={API_KEY}".format(API_KEY = iOS_KEY)
其中iOS_KEY
是我从API控制台获得的iOS
密钥。我不确定我是否选择了正确的KEY。
在此之后,我跑:
import urllib
from urllib.request import urlopen, Request
binary_data = urllib.parse.urlencode(data).encode('utf-8')
myREQ = Request(url, binary_data, {'Content-Type': 'application/json'})
urlopen(myREQ)
我的stacktrace是:
HTTPError Traceback (most recent call last)
<ipython-input-167-aa1da44f3dff> in <module>()
----> 1 urlopen(myREQ)
/Users/eron/anaconda/lib/python3.5/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
160 else:
161 opener = _opener
--> 162 return opener.open(url, data, timeout)
163
164 def install_opener(opener):
/Users/eron/anaconda/lib/python3.5/urllib/request.py in open(self, fullurl, data, timeout)
469 for processor in self.process_response.get(protocol, []):
470 meth = getattr(processor, meth_name)
--> 471 response = meth(req, response)
472
473 return response
/Users/eron/anaconda/lib/python3.5/urllib/request.py in http_response(self, request, response)
579 if not (200 <= code < 300):
580 response = self.parent.error(
--> 581 'http', request, response, code, msg, hdrs)
582
583 return response
/Users/eron/anaconda/lib/python3.5/urllib/request.py in error(self, proto, *args)
507 if http_err:
508 args = (dict, 'default', 'http_error_default') + orig_args
--> 509 return self._call_chain(*args)
510
511 # XXX probably also want an abstract factory that knows when it makes
/Users/eron/anaconda/lib/python3.5/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
441 for handler in handlers:
442 func = getattr(handler, meth_name)
--> 443 result = func(*args)
444 if result is not None:
445 return result
/Users/eron/anaconda/lib/python3.5/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
587 class HTTPDefaultErrorHandler(BaseHandler):
588 def http_error_default(self, req, fp, code, msg, hdrs):
--> 589 raise HTTPError(req.full_url, code, msg, hdrs, fp)
590
591 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 400: Bad Request
有人遇到过类似的问题吗?