[英]How to create and show a folder in google drive using python script?
This code is showing the id
of the folder_name
but it is not showing up the folder_name
in google drive.此代码显示了
folder_name
的id
,但未在 google 驱动器中显示folder_name
。 My purpose is to create and show the folder_name
in google drive.我的目的是在谷歌驱动器中创建并显示
folder_name
名称。 How to do it?怎么做?
import httplib2
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
scope = 'https://www.googleapis.com/auth/drive'
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
http = httplib2.Http()
drive_service = build('drive', 'v3', http=credentials.authorize(http))
def createFolder(name):
file_metadata = {
'name': name,
'mimeType': 'application/vnd.google-apps.folder'
}
file = drive_service.files().create(body=file_metadata,
fields='id').execute()
print('Folder ID: %s' % file.get('id'))
createFolder('folder_name')
In your script, the folder is created by the service account.在您的脚本中,该文件夹由服务帐户创建。 I think that this is the reason of your issue.
我认为这是您的问题的原因。 The Google Drive of service account is different from your Google Drive.
服务帐户的 Google Drive 与您的 Google Drive 不同。 By this, the folder created by the service account cannot be seen in your Google Drive using the browser.
这样,使用浏览器就无法在您的 Google Drive 中看到由服务帐户创建的文件夹。 When you want to see the folder created by the service account at the browser, how about the following flow?
当你想在浏览器上看到服务账户创建的文件夹时,下面的流程怎么样?
credentials.json
. credentials.json
文件中确认。 By this flow, I think that your goal can be achieved.通过这个流程,我认为您的目标可以实现。 For this, please modify your script as follows.
为此,请按如下方式修改您的脚本。
file_metadata = {
'name': name,
'mimeType': 'application/vnd.google-apps.folder'
}
file_metadata = {
'name': name,
'mimeType': 'application/vnd.google-apps.folder',
'parents': ['### folder ID ###']
}
### folder ID ###
with your created folder ID in your Google Drive.### folder ID ###
替换为您在 Google 云端硬盘中创建的文件夹 ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.