title: How do I insert a row in my google fusion table using Python
body: <p>I am working on a project and part of it involves inserting rows in to a Google Fusion Table for the Project from a python script. I have spent the last couple days trying to figure out just how to do that and I am officially confused.</p>
  
  <p>My research seems to indicate that I need to use Oauth 2.0 to access the API. In doing so I can successfully get an access token but I can't seem to successfully get a refresh token. I'm not sure if this is going to hamper my ability to successfully integrate access to my Fusion Table with my Python code. </p>
  
  <p>The second problem I am having is that I don't really understand how exactly to code inserting a row in my table. Most of the material I have found on it is from the deprecated Fusion Tables SQL API and I don't fully understand the new way of doing it.</p>
  
  <p>I'm a beginner at this sort of thing and any direction to help me is very much appreciated!</p>
  
  <p>Edit:
  So the code I have working so far looks like this:</p>
  
  <pre><code>client_id = "&lt;client_i&gt;"
  client_secret = "&lt;client_secret&gt;"
  table_id = "&lt;table_id&gt;"
  
  access_token = ""
  refresh_token = "&lt;refresh_token&gt;"
  
  #   the refresh token is used to request a new access token
  data = urllib.urlencode({
    'client_id': client_id,
    'client_secret': client_secret,
    'refresh_token': refresh_token,
    'grant_type': 'refresh_token'})
  request = urllib2.Request(
    url='https://accounts.google.com/o/oauth2/token',
    data=data)
  request_open = urllib2.urlopen(request)
  response = request_open.read()
  request_open.close()
  tokens = json.loads(response)
  access_token = tokens['access_token']
  
  #   Read the table
  request_read = urllib2.Request(
    url='https://www.google.com/fusiontables/api/query?%s' % \
      (urllib.urlencode({'access_token': access_token,
                         'sql': 'SELECT * FROM table_id'})))
  request_open = urllib2.urlopen(request_read)
  response = request_open.read()
  request_open.close()
  print response
  </code></pre>
  
  <p>And my code for trying to insert a new row into my table:</p>
  
  <pre><code>date = str(datetime.now().date())
  time = str(datetime.now().time())
  query = 'INSERT INTO table_id (Date,Time,Saskatoon,Regina,MeadowLake)VALUES(date,time,60.01,60.02,59.99)'
  data = urllib2.Request(
    url='https://www.google.com/fusiontables/api/query?%s' % \
      (urllib.urlencode({'access_token': access_token,
                         'sql': query})))
  request_open = urllib2.urlopen(data)
  </code></pre>
  
  <p>When I run this i get</p>
  
  <blockquote>
    <p>HTTP Error 400: HTTP GET can only be used for select queries.</p>
  </blockquote>
  
  <p>I am know I'm supposed to be making a POST not a GET for the INSERT, I'm just not sure what needs to change in my code for that to happen. 
  Sorry for being a noob.</p>
  
  <p>2ND EDIT: </p>
  
  <p>Sorry for making this longer but I feel it is pertinent to show where I've gotten so far. I switched to the library requests and things have gotten somewhat easier however I still haven't successfully made a POST. My new code for importing rows is as follows:</p>
  
  <pre><code>def importRows(self):
      print 'IMPORT ROWS'
      date = str(datetime.now().date())
      time = str(datetime.now().time())
      data = {'Date': date,
              'Time': time,
              'Saskatoon': '60.01',
              'Regina': '59.95'}
      url = 'https://www.googleapis.com/upload/fusiontables/v1/tables/%s/import/%s' % \
            (tableid, self.params) # self.params is access token
      importRow = requests.post(url, params=data)
  
      print importRow.status_code
      print importRow.text
  </code></pre>
  
  <p>Which gives me </p>
  
  <pre><code>400
  {
   "error": {
    "errors": [
     {
      "domain": "fusiontables",
      "reason": "badImportInputEmpty",
      "message": "Content is empty."
     }
    ],
    "code": 400,
    "message": "Content is empty."
   }
  }
  </code></pre>