2012-03-28 85 views
0

我試圖在Google Places API中POST check-in request。他們描述了它的方式,我有權要求本 -Google Places在Python中籤入POST請求

POST https://maps.googleapis.com/maps/api/place/check-in/json?sensor=true_or_false&key=AddYourOwnKeyHere HTTP/1.1 
Host: maps.googleapis.com 

{ 
    "reference": "place_reference" 
} 

我當前的代碼看起來是這樣的 -

def checkin(self, reference="", sensor="true"): 
    """ 
    """ 
    base_url = "https://maps.googleapis.com/maps/api/place/check-in/json" 
    params = urllib.urlencode(
      { 
       'key': self.API_KEY, 
       'sensor': sensor, 
      } 
     ) 
    post_url = base_url + "?" + params 
    headers = { 'Host': "maps.googleapis.com" } 
    data = urllib.urlencode({ 'reference': reference }) 
    req = Request(post_url, data, headers) 

    response = urllib2.urlopen(req) 
    resp = response.read() 

但我不斷收到錯誤 -

urllib2.HTTPError: HTTP Error 400: Bad Request 

我是什麼做錯了?

+0

有沒有在您的變量中的任何尾隨空白要傳遞呢? – 2012-03-28 07:59:00

+0

無尾隨空白。 – 2012-03-28 08:13:07

回答

1

你的問題是,API期待JSON當您發送它的字面reference: xyz

您需要發送它的JSON表示。

嘗試:

data = json.dumps({'reference': reference})

+0

解決。那真是愚蠢的我。感謝您指出。 :) – 2012-03-28 08:32:10