Excel文件未显示在Google云端硬盘中

时间:2019-07-09 17:34:22

标签: python google-app-engine google-drive-api

我正在尝试使用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')))

1 个答案:

答案 0 :(得分:1)

这个答案怎么样?

问题:

我认为您的脚本有效。但是从您的脚本来看,如果运行脚本时没有错误发生,并且client_secret.json是创建服务帐户时的文件,则我认为您正在尝试使用服务帐户上载文件。服务帐户不是您的帐户。因此,当您使用服务帐户将文件上传到Google云端硬盘时,该文件即被创建到服务帐户的云端硬盘中。这样,上传的文件将无法在您的Google云端硬盘上看到。

解决方案

如果您想使用Google云端硬盘中的服务帐户查看上传的文件,请与您的帐户共享该文件。这样,使用服务帐户上传文件时,您可以看到上传的文件

修改点:

  • 您使用Drive API v3。因此,请将body['parents'] = [{'id':folder_id}]修改为body['parents'] = [folder_id]
  • 如果folder_id = "1g3q1OelKXDJF8H_Kc0IpgFSxz77NQobc"的文件夹ID在您的Google云端硬盘上,请将其修改为“服务帐户”上的文件夹ID。作为测试用例,我使用root作为文件夹ID。
  • 通过服务帐户上传文件后,所有者将更改为您的帐户。这样,您可以在Google云端硬盘的根目录中看到上传的文件。

修改后的脚本:

在运行脚本之前,请将您的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()

注意:

  • 在这种情况下,您无法将文件放入Google帐户的特定文件夹。因为使用了服务帐户。
    • 如果要将文件直接上传到Google云端硬盘上的特定文件夹,请使用OAuth2而不是服务帐户。
  • 这是一个简单的修改后的脚本。因此,请根据您的情况进行修改。

参考:

如果我误解了您的问题,而这不是您想要的方向,我深表歉意。