Google Places在Python中签入POST请求

时间:2012-03-28 07:46:16

标签: python google-places-api

我正在尝试在Google Places API中发布check-in request。他们描述的方式,我必须要求 -

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

你的问题是,当你发送文字reference: xyz

时,API会期待JSON

您需要向其发送JSON表示。

尝试:

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