2015-04-08 85 views
1

目標:谷歌距離矩陣API - 錯誤= 「HTTPError:HTTP錯誤:400」

  • 99個人返回行駛時間(我開始有一個, '起源' 和 '目的地',以保證我有正確的格式),JSON格式的'起源'和'目的地'。

不過,我收到以下錯誤:「HTTPError:HTTP錯誤:400」

總之,我使用Python 2.7,這是我最初的API的嘗試,所以任何事業上的援助大大不勝感激!

FYI:我使用下面的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 

回答

0

根據谷歌的文件w如果你得到400響應代碼,它可能是無效參數badRequest。在以下情況下:

無效的參數:指示請求參數的值無效。錯誤響應中的locationType和location字段提供有關哪個值無效的信息。

如果不解決問題,請不要重試。您需要爲錯誤響應中指定的參數提供有效值。

錯誤的請求:表示查詢無效。例如,父母ID缺失或所請求的維度或指標組合無效。

如果不解決問題,請不要重試。您需要對API查詢進行更改才能使其工作。

我認爲問題出在API密鑰上。如果即使沒有API_KEY也不要求配額,您可以使用Direction API。在代碼中提供的示例代碼中,我看不到將API_KEY放入客戶端參數中。

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

試着刪除它。