PyDrive:上传的 .mp4 文件无法访问其他用户被 403 禁止

问题描述

当我从只能访问我的 Weblink API 获取链接时,我有一个脚本用于将 .mp4 文件存储到 Google Drive。我希望必须访问整个域或组织的链接,但它没有发生。我试图插入权限,但得到“文件权限不足”。下面是我的代码实现。

import os

从 pydrive2.auth 导入 GoogleAuth 从 pydrive2.drive 导入 GoogleDrive

GDriveService 类: def init(自我): self.drive = 无 self.gdrive_folder_name = 'Davros-Records'

def davros_authenticate(self):
    settings_file_path = os.path.abspath(__file__)
    settings_file_path = os.path.join(os.path.split(settings_file_path)[0],"settings.yaml")
    creds_file_path = os.path.join(os.path.split(settings_file_path)[0],"mycreds.txt")
    gauth = GoogleAuth(settings_file_path)
    # Try to load saved client credentials
    gauth.LoadCredentialsFile(creds_file_path)
    if gauth.credentials is None:
        # Authenticate if they're not there
        gauth.LocalWebserverAuth()
    elif gauth.access_token_expired:
        # Refresh them if expired
        gauth.Refresh()
    else:
        # Initialize the saved creds
        gauth.Authorize()
    # Save the current credentials to a file
    gauth.SaveCredentialsFile("mycreds.txt")

    self.drive = GoogleDrive(gauth)

def create_folder(self,folder_name):
    """
    Method is used to create a new folder in the Google drive returns folder-id.
    :param str folder_name: Contains the folder name to be created in the google drive.
    """
    folder = self.drive.CreateFile({'title': folder_name,"mimeType": "application/vnd.google-apps.folder"})
    folder.Upload()
    return folder["id"]

def check_folder_exists(self,folder_name,user,feature):
    """
    Method is used to check if a folder exists in the google drive.
    :param str folder_name: Contains the folder name to be created in the google drive.
    """
    file_list = self.drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
    folder_id = None
    for file in file_list:
        if folder_name == file['title']:
            folder_id = file['id']
    return folder_id

def upload_file_inside_folder(self,title,fid,file_path):
    """
    Method is used for uploading a file inside the specified folder on google drive.
    :param str title: uploaded file name in the google drive.
    :param str fid: contains folder id.
    :param str file_path: Contains the full path of the uploaded file in the client side.
    """
    file = self.drive.CreateFile({'title': title,"parents": [{"kind": "drive#fileLink","id": fid}]})
    file.SetContentFile(file_path)
    file.Upload()
    user_permissions = {
        'type': 'anyone','value': 'anyone','role': 'reader'
    }
    file.InsertPermission(user_permissions)
    return True

def upload_file_gdrive(self,file_path,feature):
    """
    Used to call the methods and upload file to the google drive.
    :param str file_path: Contains the full path of the uploaded file in the client side.
    """
    file_name = os.path.basename(file_path)
    folder_id = self.check_folder_exists(self.gdrive_folder_name,feature)
    if folder_id is None:
        folder_id = self.create_folder(self.gdrive_folder_name)
    self.upload_file_inside_folder(file_name,folder_id,file_path)

gdrive = GDriveService() gdrive.davros_authenticate() gdrive.upload_file_gdrive("C:\Users\srikantp\Downloads\climateV.mp4","srikantp","远程控制")

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)