简体   繁体   English

使用python将文件上传到特定的Google Drive文件夹

[英]Upload files to a specific Google Drive folder using python

I'm creating a simple web app that saves some files in my Google Drive using the Auth2.0 authentication. 我正在创建一个简单的网络应用,该应用使用Auth2.0身份验证将一些文件保存在我的Google云端硬盘中。 Everything works well when I use the code below, but I struggle with some things here. 当我使用下面的代码时,一切都工作正常,但在这里有些麻烦。 The first one is to save the content in a different specified folder. 第一个是将内容保存在另一个指定的文件夹中。 The code below save the files in the root folder and I can't figure out why.The upload_folder_id evidently has the good value, that I omitted here. 下面的代码保存在根文件夹中的文件,我想不出why.The upload_folder_id显然具有良好的价值,我这里省略。

from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/drive.file'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store, flags) \
            if flags else tools.run(flow, store)

DRIVE = build('drive', 'v3', http=creds.authorize(Http()))

upload_folder_id = <my_folder_id>
FILES = (('hello.txt', None)) # More files will be added after

for filename, mimeType in FILES:
    metadata = {'name': filename}

    if mimeType:
        metadata['mimeType'] = mimeType
    ''' I tried all this possibilities too
        metadata['parentId'] = upload_folder_id
        metadata['folderId'] = upload_folder_id
    '''
    metadata['parents'] = [{'id': upload_folder_id}]

    res = DRIVE.files().create(body=metadata, media_body=filename).execute()
    if res:
        print('Uploaded "%s" (%s)' % (filename, res['mimeType']))

The second is the authentication step. 第二步是身份验证步骤。 The first time that I connected the application, I was redirected to a google web page where I grant the API access. 第一次连接该应用程序时,我被重定向到一个Google Web页面,并在其中授予了API访问权限。 Is there a way to do this without going through the web browser to validate the access? 有没有一种方法,而无需通过网络浏览器来验证访问权限? (It's fine doing that for my local tests but I'll not be able to do this in the production server). (对于我的本地测试,这样做很好,但是我将无法在生产服务器中执行此操作)。

The code below save the files in the root folder and I can't figure out why.The upload_folder_id evidently has the good value, that I omitted here. 下面的代码将文件保存在根文件夹中,我不知道为什么.upload_folder_id显然具有很好的价值,在此省略。

parents is supposed to be a string array, so there's no need for the id property to be there; parents应该是一个字符串数组,因此不需要id属性; just specify the parent folderId and I think you're good to go 只需指定父folderId,我认为您很好

Is there a way to do this without going through the web browser to validate the access? 有没有一种方法,而无需通过网络浏览器来验证访问权限?

The consent page is critical for the authentication process of the user, I don't think scraping can be a solution here. 同意页面对于用户的身份验证过程至关重要,我认为在这里抓取不是解决方案。 An alternative I can think of is for you to use Service Accounts, which is a server-to-server implementation so that the users don't need to login. 我可以想到的另一种选择是让您使用服务帐户,这是服务器到服务器的实现,因此用户无需登录。 The drawback here though is for the user doesn't own the data, the service account does. 尽管这里的缺点是用户不拥有数据,服务帐户却拥有数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何使用 folderId 将文件上传到 Google Drive 中的特定文件夹 - Python: How to upload files to a specific folder in Google Drive using folderId 如何使用 Python 将文件插入/上传到 Google Drive API v3 中的特定文件夹 - How can I insert/upload files to a specific folder in Google Drive API v3 using Python 如何使用python和Google云端硬盘上传到文件夹并替换文件? - how to upload to folder and replace files using python and Google Drive? 如何使用 Google Drive API 和 Python 将文件上传到特定文件夹? - How to upload file into specific folder with Google Drive API and Python? 如何使用 python 驱动器 api 将 csv 文件上传到谷歌驱动器 - How to upload csv files to google drive using python drive api Python-如何使用 Google Drive API 将文件上传到一个文件夹? - Python- How to upload files to one folder using Google Drive API? 在谷歌驱动器上创建一个文件夹(如果不存在)并使用 Python 脚本将文件上传到它 - Create a folder (if not exists) on google drive and upload a file to it using Python script Python-使用gspread在Google云端硬盘的特定文件夹中创建电子表格 - Python - Using gspread to create a spreadsheet in a specific folder in Google Drive 如何使用 Python 通过 api 将文件上传到谷歌驱动器? - How to upload files into google drive via api using Python? 如何使用 python 请求使用谷歌驱动器 API 上传和删除文件 - How to upload and delete files with google drive API using python requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM