我正在尝试使用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')))
答案 0 :(得分:1)
这个答案怎么样?
我认为您的脚本有效。但是从您的脚本来看,如果运行脚本时没有错误发生,并且client_secret.json
是创建服务帐户时的文件,则我认为您正在尝试使用服务帐户上载文件。服务帐户不是您的帐户。因此,当您使用服务帐户将文件上传到Google云端硬盘时,该文件即被创建到服务帐户的云端硬盘中。这样,上传的文件将无法在您的Google云端硬盘上看到。
如果您想使用Google云端硬盘中的服务帐户查看上传的文件,请与您的帐户共享该文件。这样,使用服务帐户上传文件时,您可以看到上传的文件
body['parents'] = [{'id':folder_id}]
修改为body['parents'] = [folder_id]
。folder_id = "1g3q1OelKXDJF8H_Kc0IpgFSxz77NQobc"
的文件夹ID在您的Google云端硬盘上,请将其修改为“服务帐户”上的文件夹ID。作为测试用例,我使用root
作为文件夹ID。在运行脚本之前,请将您的Google帐户的电子邮件地址设置为'emailAddress': '### Email address of your Google account ###'
。
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 = "root" # Modified from "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'] = [folder_id] # Modified
# 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')))
# I added below script.
user_permission = {
'type': 'user',
'role': 'owner',
'emailAddress': '### Email address of your Google account ###'
}
drive_api.permissions().create(
fileId=fiahl.get('id'),
body=user_permission,
transferOwnership=True,
).execute()
如果我误解了您的问题,而这不是您想要的方向,我深表歉意。