[英]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.