【问题标题】:Google Places Checkin POST request in PythonPython中的Google Places Checkin POST请求
【发布时间】:2012-03-28 07:46:16
【问题描述】:

我正在尝试在 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

我做错了什么?

【问题讨论】:

  • 您传递的变量中是否有任何尾随空格?

标签: python google-places-api


【解决方案1】:

您的问题是,当您向 API 发送文字 reference: xyz 时,API 需要 JSON

您需要将 JSON 表示形式发送给它。

试试:

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

【讨论】:

  • 已解决。那真是让我傻眼了。感谢您指出。 :)
猜你喜欢
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多