【发布时间】:2019-07-10 06:43:01
【问题描述】:
我正在尝试使用 Python 中的 API 从 CSV 创建一个新的 Google 表格电子表格,并且我的代码运行,但创建的表格没有显示在我的 Google 云端硬盘中的任何位置。知道我做错了什么吗?
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
#Set up a credentials object I think
creds =
ServiceAccountCredentials.from_json_keyfile_name('client_secret.json',
['https://www.googleapis.com/auth/drive'])
#Now build our api object, thing
drive_api = build('drive', 'v3', credentials=creds)
folder_id = "1g3q1OelKXDJF8H_Kc0IpgFSxz77NQobc"
file_name = "1505"
mimeType = "application/vnd.google-apps.spreadsheet"
print("Uploading file " + file_name + "...")
file_metadata = {
'name': file_name,
'mimeType': mimeType
}
#We have to make a request hash to tell the google API what we're giving it
body = {'name': file_name, 'mimeType': mimeType}
body['parents'] = [{'id':folder_id}]
#Now create the media file upload object and tell it what file to upload,
#in this case 'test.html'
media = MediaFileUpload('1505.csv', mimetype = 'text/csv')
#Now we're doing the actual post, creating a new file of the uploaded type
fiahl = drive_api.files().create(body=body, media_body=media).execute()
#Because verbosity is nice
print("Created file '%s' id '%s'." % (fiahl.get('name'), fiahl.get('id')))
【问题讨论】:
标签: python google-app-engine google-drive-api