PyDrive复制文件经过身份验证的用户无法复制此文件(Python)

时间:2018-01-31 12:08:23

标签: python google-api google-drive-api google-api-python-client pydrive

抱歉我的英文。我尝试将文件复制到另一个文件夹。但我有错误:

  

googleapiclient.errors.HttpError:https://www.googleapis.com/drive/v2/files/1yH7LZ54uaiOl4T0J5UoJ9zb30cVKtW19/copy?alt=json   返回“经过身份验证的用户无法复制此文件。”>

我的代码:

parent_folder = 'sdadasdasdasdasd23123123123-9'

        drive = DriveBox()
        list_files = drive.list_files_from_parent_folder(parent_folder)

        new_folder = drive.create_folder("228", parent_folder)
        for file in list_files:

            drive.g_drive.auth.service.files().copy(fileId=file['id'],
                                            body={"parents": [{"kind": "drive#fileLink",
                                                               "id": new_folder['id']}], 'title': file['title']})\
                .execute()

类DriveBox

class DriveBox:
    g_drive = None
    instance = None

    def __new__(cls, *args, **kwargs):
        if cls.instance is None:
            gauth = GoogleAuth()
            try:
                gauth.CommandLineAuth()
            except (AuthenticationError, RefreshError) as e:
                print(e)

            cls.g_drive = GoogleDrive(gauth)
            cls.instance = super(DriveBox, cls).__new__(cls)
        return cls.instance

我的设置.yaml

client_config_backend: settings
client_config:
  client_id: 1111111111111111
  client_secret: 1111111111111111

save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json

get_refresh_token: True

oauth_scope:
  - https://www.googleapis.com/auth/drive
  - https://www.googleapis.com/auth/drive.file
  - https://www.googleapis.com/auth/drive.install
  - https://www.googleapis.com/auth/drive.appfolder
  - https://www.googleapis.com/auth/drive.metadata

1 个答案:

答案 0 :(得分:0)

  

经过身份验证的用户无法复制此文件。

就是这个意思。您正在尝试打开用户拥有的私有文件,而不首先对用户进行身份验证。

我建议您查看云端硬盘教程,了解如何完成身份验证过程Python quickstart

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'drive-python-quickstart.json')

    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

附注:对于您要求的范围合理https://www.googleapis.com/auth/drive会让您访问所有已经加倍的内容是无意义的