将文件上传到GoogleTeam Drive

时间:2019-05-16 16:25:17

标签: python python-3.x google-drive-api

我想将文件上传到团队驱动器,我在团队驱动器中有经理规则,并且我使用的是api版本2。

我尝试了下面这段代码中看到的代码:https://stackoverflow.com/a/52171019

from __future__ import print_function

from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.http import MediaFileUpload
import glob
import os
import sys


args = tools.argparser.parse_args()
args.noauth_local_webserver = True

SCOPES = 'https://www.googleapis.com/auth/drive'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_id.json', SCOPES)
    creds = tools.run_flow(flow, store, args)
DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))


file_name = glob.glob('*.mp4')[0]
print("Uploading ...")
media_body = MediaFileUpload(file_name , resumable=True)
body = {
    'parents': '0AE**********',
    'title': file_name,
    'description': 'Uploaded By Poorya'}
file = DRIVE.files().insert( body=body, media_body=media_body ,supportsTeamDrives=True , fields='id, alternateLink')
response = None
while response is None:
  status, response = file.next_chunk()
  if status:
    #print ("\rUploaded %d%%." % int(status.progress() * 100))
     sys.stdout.write("\rUploaded %{0}".format(int(status.progress() * 100)))
     sys.stdout.flush()
if file:
    print("\n"+file_name + " uploaded successfully")
prem = DRIVE.permissions().insert( fileId=response.get('id'), body={'type': 'anyone', 'value': 'anyone', 'role': 'reader', 'withLink': True}).execute()
print ("Your sharable link: "+ "https://drive.google.com/file/d/" + response.get('id')+'/view')

此代码有效,但它不会上传到团队驱动器,而是上传到Google驱动器。

0 个答案:

没有答案