2012-03-28 81 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