这是 google 中给出的示例代码的更改代码,用于通过 python 访问 Fusion 表。我想在融合表中创建表,然后插入数据......使用发布请求我设法编写以下代码通过发送发布请求来创建表。但这会返回“无效凭据”错误,并且位置是 > Header。请帮助...或建议一个简单的代码来创建表并插入 Fusion 表(数据在 txt 文件中)。我对此很陌生。请帮助。
完整代码:
#!/usr/bin/python
# Copyright (C) 2011 Google Inc.
""" Demostrates use of the new Fusion Tables API
"""
import urllib2, urllib, simplejson, sys, httplib
client_id = "996027904153.apps.googleusercontent.com"
client_secret = "secret-given"#
redirect_uri = "my localhost"
api_key = "my api_key is given"
tableid = "1jCqbPu5PjNsczx7cDl3y9EJtSUgNePbvXT7-x70"
class RunAPITest:
def __init__(self):
self.access_token = ""
self.params = ""
def main(self):
print "copy and paste the url below into browser address bar and hit enter"
print "https://accounts.google.com/o/oauth2/auth?%s%s%s%s" % \
("client_id=%s&" % (client_id),"redirect_uri=%s&" % (redirect_uri),
"scope=https://www.googleapis.com/auth/fusiontables&","response_type=code")
code = raw_input("Enter code (parameter of URL): ")
data = urllib.urlencode({
"code": code,
"client_id": client_id,
"client_secret": client_secret,
"redirect_uri": redirect_uri,
"grant_type": "authorization_code"
})
serv_req = urllib2.Request(url="https://accounts.google.com/o/oauth2/token",
data=data)
serv_resp = urllib2.urlopen(serv_req)
response = serv_resp.read()
tokens = simplejson.loads(response)
access_token = tokens["access_token"]
print access_token
self.access_token = access_token
print "CREATE TABLE"
data='''{ "name": "Insects","columns": [{"name": "Species","type": "STRING"},{"name": "Elevation","type": "NUMBER"},{"name": "Year","type": "DATETIME"}],"description": "Insect Tracking Information.","isExportable": "true"}'''
headers={}
headers['Authorization']=access_token
headers['Content-Type']='application/json'
print headers
response = self.runRequest("POST","/fusiontables/v1/tables/",data,headers)
json_response = simplejson.loads(response)
return json_response
def runRequest(self, 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()
print response.status, response.reason
response = response.read()
print response
return response
if __name__ == "__main__":
api_test = RunAPITest()
api_test.main()