Excel文件未显示在Google云端硬盘中 [英] Excel File Not Showing Up in Google Drive

查看:119
本文介绍了Excel文件未显示在Google云端硬盘中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python中的API从CSV创建新的Google表格电子表格,并且我的代码可以运行,但是创建的表格无法显示在我的Google云端硬盘中的任何位置.知道我在做什么错吗?

I am trying to create a new Google Sheets spreadsheet from a CSV using the API in Python and my code runs but the created Sheet does not show up in my Google Drive anywhere. Any idea what I am doing wrong?

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')))

推荐答案

此答案如何?

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

I think that your script works. But from your script, if no error occurs when you run the script and client_secret.json is the file when the Service Account is created, I think that you are trying to upload a file using Service Account. Service Account is not your account. So when you upload a file to Google Drive using Service Account, the file is created to the Drive of the Service Account. By this, the uploaded file cannot be seen on your Google Drive.

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

When you want to see the uploaded file using the Service Account at your Google Drive, please share the file with your account. By this, when the file is uploaded using Service Account, you can see the uploaded file

  • 您使用Drive API v3.因此,请将body['parents'] = [{'id':folder_id}]修改为body['parents'] = [folder_id].
  • 如果folder_id = "1g3q1OelKXDJF8H_Kc0IpgFSxz77NQobc"的文件夹ID在您的Google云端硬盘上,请将其修改为服务帐户"上的文件夹ID.作为测试用例,我使用root作为文件夹ID.
  • 通过服务帐户上传文件后,所有者将更改为您的帐户.这样,您可以在Google云端硬盘的根目录中看到上传的文件.
  • You use Drive API v3. So please modify body['parents'] = [{'id':folder_id}] to body['parents'] = [folder_id].
  • If the folder ID of folder_id = "1g3q1OelKXDJF8H_Kc0IpgFSxz77NQobc" is on your Google Drive, please modify this to the folder ID on Service Account. As a test case, I used root as the folder ID.
  • After the file is uploaded by the Service account, the owner is changed to your account. By this, you can see the uploaded file at the root of your Google Drive.

在运行脚本之前,请将您的Google帐户的电子邮件地址设置为'emailAddress': '### Email address of your Google account ###'.

Before you run the script, please set the email address of your Google account to '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而不是服务帐户.
    • Note:

      • In this case, you cannot put the file to the specific folder of your Google Account. Because the Service Account is used.
        • If you want to directly upload the file to the specific folder on your Google Drive, please use OAuth2 instead of Service account.
          • Service accounts
          • Permissions: create

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

          If I misunderstood your question and this was not the direction you want, I apologize.

          这篇关于Excel文件未显示在Google云端硬盘中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆