我试图用下面的代码从运行django的服务器创建一个fusion表,但是它一直失败,出现了一个500服务器错误。你知道吗
scope = ['https://www.googleapis.com/auth/fusiontables']
credentials = ServiceAccountCredentials.from_json_keyfile_name(EE_CREDENTIALS, scope)
http_auth = credentials.authorize(Http())
def create_table(name, description, columns, data=None):
ft_service = build('fusiontables', 'v2', http_auth)
body = dict(name=name, description=description, columns=columns)
table = ft_service.table()
result = table.insert(body=body).execute(num_retries=3) # failing here
if data is not None:
if not os.path.exists(TEMP_DIRPATH):
os.makedirs(TEMP_DIRPATH)
keys = data[0].keys()
if len(columns) != len(keys):
raise ValueError("mismatch in number of columns")
filename = TEMP_DIRPATH + str(result.tableId) + ".csv"
with open(filename, 'wb') as upload_file:
dict_writer = csv.DictWriter(upload_file, keys)
dict_writer.writeheader()
dict_writer.writerows(data)
ft_service.importRows(tableId=result.tableId, media_body=filename, startLine=1, isStrict=True,
encoding="auto-detect", delimiter=",").execute(num_retries=3)
return result.tableId
def test_create_table(filename):
data = []
columns = []
with open(filename, 'rb') as csvfile:
reader = csv.reader(csvfile)
for row_index, row in enumerate(reader):
if row_index == 0:
header = list(row)
for col_index, col in enumerate(row):
if col_index == 24 or col_index == 25:
columns.append({"name": header[col_index], "type": "LOCATION"})
else:
columns.append({"name": header[col_index], "type": "STRING"})
else:
# 24 and 25 are latitude and longitude.
if caught(float, row[24]) or caught(float, row[25]):
continue
properties = {}
for col_index, col in enumerate(row):
# 21 is ch4
if col_index == 21:
properties[header[col_index]] = 0 if caught(float, col) else float(col)
else:
properties[header[col_index]] = col
data.append(properties)
table_id = create_table('chino-20150110', 'locality = chino and date = 20150110', columns, None)
print "created fusion table id is " + str(table_id)
test_create_table('C:/Users/JohnsonCharles/Desktop/chino-20150110.csv')
我得到的错误是:
googleapiclient.errors.HttpError: <HttpError 500 when requesting https://www.googleapis.com/fusiontables/v2/tables?alt=json returned "Backend Error">
我还想知道它是如何知道使用哪个google驱动器的。既然我是从后端创建fusion表,那么如何让我的应用程序使用特定的google驱动器来创建fusion表呢?你知道吗
This post帮助解决了这个问题。
isExportable
属性在尝试使用服务帐户创建融合表时必须显式设置为true。帖子还解释了我所关心的使用哪个驱动器的问题。默认情况下,它将是创建融合表的用户的驱动器—在本例中是服务帐户。它清楚地解释了如何授予其他用户权限,但帖子是用Java编写的。我试着在python中实现同样的功能,下面是我的更新代码,供任何人使用 同样的问题。你知道吗有关驱动器服务参数的详细信息,请参见this链接。注意,我在这里使用的驱动api是
v3
,如果您想更改所创建的融合表的可见性,您必须将allowFileDiscovery
设置为False
,将type
设置为domain
或anyone
。默认情况下,它是私有的,但是您与之共享的用户可以访问它。你知道吗相关问题 更多 >
编程相关推荐