简体   繁体   中英

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. 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.

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. 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.

parents is supposed to be a string array, so there's no need for the id property to be there; just specify the parent folderId and I think you're good to go

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM