Google Distance Matrix API - 错误=" HTTPError:HTTP错误:400"

时间:2015-04-08 14:44:57

标签: python json google-maps-api-3

目标:

  • 返回99个人的驾驶时间(我从一个'起点'和'目的地'开始,以确保我的格式正确),'起源'和'目的地'以JSON格式。

但是,我收到以下错误:“HTTPError:HTTP Error:400”

总的来说,我使用的是Python 2.7,这是我最初的API尝试,因此非常感谢您对该事业的任何帮助!

仅供参考:我使用以下GitHub作为构建代码的蓝图: https://github.com/googlemaps/google-maps-services-python/blob/master/googlemaps/distance_matrix.py

代码:

import googlemaps
from googlemaps import convert
from googlemaps.convert import as_list


#API key
key = 'xxxxx'
client = googlemaps.Client(key)


#establishes: drive duration, english, non-metric measurements
def distance_matrix(client, origins, destinations,
                    mode="driving", language="en", avoid=None, units="imperial",
                    departure_time="calendar.timegm(time.gmtime())", arrival_time=None, transit_mode=None,transit_routing_preference=None):

    #establishes "origin" and "destinations" geoInfo via the .json structure
    params = {
    "origins": '1218 3rd St, Santa Monica, 90401, CA',
    "destinations": '90278'
}

    if mode:
        # NOTE(broady): the mode parameter is not validated by the Maps API
        # server. Check here to prevent silent failures.
        if mode not in ["driving", "walking", "bicycling", "transit"]:
            raise ValueError("Invalid travel mode.")
        params["mode"] = mode

    if language:
        params["language"] = language

    if avoid:
        if avoid not in ["tolls", "highways", "ferries"]:
            raise ValueError("Invalid route restriction.")
        params["avoid"] = avoid

    if units:
        params["units"] = units

    if departure_time:
        params["departure_time"] = convert.time(departure_time)

    if arrival_time:
        params["arrival_time"] = convert.time(arrival_time)

    if departure_time and arrival_time:
        raise ValueError("Should not specify both departure_time and"
                         "arrival_time.")

    if transit_mode:
        params["transit_mode"] = convert.join_list("|", transit_mode)

    if transit_routing_preference:
        params["transit_routing_preference"] = transit_routing_preference

    return client._get("/maps/api/distancematrix/json", params)


print distance_matrix(client, "origins", "destinations")

完成错误消息追溯:

HTTPError                                 Traceback (most recent call last)
<ipython-input-23-3127b996dc24> in <module>()
     58 
     59 
---> 60 print distance_matrix(client, "origins", "destinations")
     61 

<ipython-input-23-3127b996dc24> in distance_matrix(client, origins, destinations, mode, language, avoid, units, departure_time, arrival_time, transit_mode, transit_routing_preference)
     55         params["transit_routing_preference"] = transit_routing_preference
     56 
---> 57     return client._get("/maps/api/distancematrix/json", params)
     58 
     59 

C:\Users\AppData\Local\Continuum\Anaconda2\lib\site-packages\googlemaps\client.pyc in _get(self, url, params, first_request_time, retry_counter, base_url, accepts_clientid, extract_body)
    179             if extract_body:
    180                 return extract_body(resp)
--> 181             return self._get_body(resp)
    182         except googlemaps.exceptions._RetriableRequest:
    183             # Retry request.

C:\Users\AppData\Local\Continuum\Anaconda2\lib\site-packages\googlemaps\client.pyc in _get_body(self, resp)
    187     def _get_body(self, resp):
    188         if resp.status_code != 200:
--> 189             raise googlemaps.exceptions.HTTPError(resp.status_code)
    190 
    191         body = resp.json()

HTTPError: HTTP Error: 400

1 个答案:

答案 0 :(得分:0)

根据Google的文档,当您获得400响应代码时,它可能是无效参数 badRequest 。如果:

参数无效:表示请求参数的值无效。错误响应中的locationType和location字段提供有关哪个值无效的信息。

如果不解决问题,请勿重试。您需要为错误响应中指定的参数提供有效值。

错误请求:表示查询无效。例如,缺少父ID或请求的维度或指标的组合无效。

如果不解决问题,请勿重试。您需要对API查询进行更改才能使其生效。

我认为问题在于API密钥。如果即使没有API_KEY也不请求配额,也可以使用Direction API。在代码中提供的示例代码中,我看不到API_KEY被放在client参数中。

#API key
key = 'xxxxx'
client = googlemaps.Client(key)

尝试删除此内容。