适用于插入和更新列的Google Fusion表API

时间:2015-03-25 21:25:51

标签: python google-api google-fusion-tables

我正在尝试更新我现有的融合表,就像在给定的python Scripts 中一样。它适用于方法" GET"但它不适用于方法" PUT"和" POST"。我的URL请求功能是:

mport urllib2, urllib, simplejson, sys, httplib 

def runRequest(method, url, data=None, headers=None):

   request = httplib.HTTPSConnection("www.googleapis.com")

   if data and headers: 
       request.request(method, url, data, headers)
   else: 
       request.request(method, url)

   response = request.getresponse()

   response = response.read()

   return response

我定义了

  

getColumns和insertColumn as:

def getColumns():
     print "GET COLUMNS"
     runRequest(
      "GET",
      "/fusiontables/v2/tables/%s/columns/%s" % \
        (tableid,  params))

  def insertColumn(name, data_type):
    print "Insert Column"
    data = '''{
    "tableId": %s,
      "name": %s,
      "type":%s
    }''' % (tableid, name, data_type)
    response = runRequest(
      "POST",
      "/fusiontables/v2/tables/%s/columns%s" % \
       (tableid, params), data, headers={'Content-Type':'application/json'})
    json_response = simplejson.loads(response)
    print json_response

getColumn()方法看起来很好:

GET COLUMNS 

200 OK 
{  "kind": "fusiontables#columnList", 

 "totalItems": 28,  "items": [   {    "kind": "fusiontables#column",   
 "columnId": 0,    "name": "VDC",    "type": "STRING",   
 "formatPattern": "NONE",    "validateData": false   }}

我从insertColumn()方法得到以下错误:

Insert Column
400 Bad Request
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "Parse Error"
   }
  ],
  "code": 400,
  "message": "Parse Error"
 }
}

我认为这是由于错误的runRequest()方法或httpconnection工作不正常。你有什么建议吗?有没有其他方法可以解决这个问题?感谢。

1 个答案:

答案 0 :(得分:0)

错误信息是一个很好的起点。

  

解析错误

如果您确实检查了数据输出,那么您会发现它不是有效的JSON。

tableid, name, data_type = ('foo', 'bar', 'baz')
data = '''{
"tableId": %s,
  "name": %s,
  "type":%s
}''' % (tableid, name, data_type)
print data
  

' {       " tableId":foo,         " name":bar,         "输入":baz   }'

字符串值需要在它们周围引用。更好的版本就是这样的。

tableid, name, data_type = ('foo', 'bar', 'baz')
data = {
"tableId": tableid,
  "name": name,
  "type": data_type
}
print simplejson.dumps(data)
  

{"输入":" baz"," tableId":" foo"," name":& #34;杆"}