尝试导出 GDrive 工作区文件时出现文件权限不足错误

问题描述 投票:0回答:2

尝试导出 GDrive 工作区文件时遇到不够文件权限错误。

错误:

googleapiclient.http:Encountered 403 Forbidden with reason "insufficientFilePermissions"

<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/{File_ID}/export?mimeType=application%2Fvnd.oasis.opendocument.presentation&alt=media returned "
The user does not have sufficient permissions for this file.". Details: "[{
'message': 'The user does not have sufficient permissions for this file.', 
'domain': 'global', 
'reason': 'insufficientFilePermissions'
}]">

我正在使用在Link找到的python

代码

为了测试代码,我在我的 GDrive 帐户上配置了 service_account。我可以下载 Google 工作区文件并且工作正常。但是,相同的代码会为客户 GDrive 引发上述错误。我正确配置了service_account,以便能够下载除worksapce 之外的其余GDrive 文件。

分享,GDrive工作区文件结构为

{
    'kind': 'drive#file',
    'copyRequiresWriterPermission': False,
    'writersCanShare': True,
    'viewedByMe': True,
    'mimeType': 'application/vnd.google-apps.presentation',
    'exportLinks': {
        'application/vnd.oasis.opendocument.presentation': 'https://docs.google.com/feeds/download/presentations/Export?id={file_id}&exportFormat=odp',
        'application/pdf': 'https://docs.google.com/feeds/download/presentations/Export?id={file_id}&exportFormat=pdf',
        'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'https://docs.google.com/feeds/download/presentations/Export?id={file_id}&exportFormat=pptx',
        'text/plain': 'https://docs.google.com/feeds/download/presentations/Export?id={file_id}&exportFormat=txt'
    },
    'parents': ['root'],
    'thumbnailLink': 'https://docs.google.com/feeds/vt?gd=true&id={file_id}&v=2&s=AMedNnoAAAAAZH2QVm6sssfJMwvO4_wZtTxvWk9-VGZP&sz=s220',
    'iconLink': 'https://drive-thirdparty.googleusercontent.com/16/type/application/vnd.google-apps.presentation',
    'shared': False,
    'lastModifyingUser': {
        'displayName': '{user_name}',
        'kind': 'drive#user',
        'me': True,
        'permissionId': '{permissionId}',
        'emailAddress': '{email_id}'
    },
    'owners': [{
        'displayName': '{user_name}',
        'kind': 'drive#user',
        'me': True,
        'permissionId': '{permissionId}',
        'emailAddress': '{email_id}'
    }],
    'webViewLink': 'https://docs.google.com/presentation/d/{file_id}/edit?usp=drivesdk',
    'viewersCanCopyContent': True,
    'permissions': [{
        'id': '{permissionId}',
        'displayName': '{user_name}',
        'type': 'user',
        'kind': 'drive#permission',
        'emailAddress': '{email_id}',
        'role': 'owner',
        'deleted': False,
        'pendingOwner': False
    }],
    'hasThumbnail': True,
    'spaces': ['drive'],
    'id': '{file_id}',
    'name': '{file name}',
    'starred': False,
    'trashed': False,
    'explicitlyTrashed': False,
    'createdTime': '2022-02-22T04:05:47.677Z',
    'modifiedTime': '2022-02-22T04:05:51.475Z',
    'modifiedByMeTime': '2022-02-22T04:05:51.475Z',
    'viewedByMeTime': '2022-02-22T04:05:51.475Z',
    'quotaBytesUsed': '0',
    'version': '275',
    'ownedByMe': True,
    'isAppAuthorized': False,
    'capabilities': {
        'canChangeViewersCanCopyContent': True,
        'canEdit': True,
        'canCopy': True,
        'canComment': True,
        'canAddChildren': False,
        'canDelete': True,
        'canDownload': True,
        'canListChildren': False,
        'canRemoveChildren': False,
        'canRename': True,
        'canTrash': True,
        'canReadRevisions': True,
        'canChangeCopyRequiresWriterPermission': True,
        'canMoveItemIntoTeamDrive': True,
        'canUntrash': True,
        'canModifyContent': True,
        'canMoveItemOutOfDrive': True,
        'canAddMyDriveParent': False,
        'canRemoveMyDriveParent': True,
        'canMoveItemWithinDrive': True,
        'canShare': True,
        'canMoveChildrenWithinDrive': False,
        'canModifyContentRestriction': True,
        'canChangeSecurityUpdateEnabled': False,
        'canAcceptOwnership': False,
        'canReadLabels': True,
        'canModifyLabels': True
    },
    'thumbnailVersion': '2',
    'modifiedByMe': True,
    'permissionIds': ['{permissionId}'],
    'linkShareMetadata': {
        'securityUpdateEligible': False,
        'securityUpdateEnabled': True
    }
python google-drive-api google-workspace google-api-python-client service-accounts
2个回答
2
投票

用户对此文件没有足够的权限。

确切地说,您正在验证的用户没有访问该文件的权限。您正在使用服务帐户,因此您有两个选择。

打开服务帐户文件,找到服务帐户电子邮件地址。它是唯一一个带有 @ 的应用程序,请转到 google Drive Web 应用程序并与服务帐户共享目录,就像您与任何其他用户一样。

第二个选项,因为您正在使用 Google Workspace,这可能是最好的选择,即让您的工作区域的管理员为您的服务帐户配置域范围委派。这将授予服务帐户访问权限以模拟工作区域上的任何用户。然后,您可以在代码中指定要模拟哪个用户。

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    SERVICE_ACCOUNT_FILE_PATH,
    scopes=SCOPES)

credentials = credentials.create_delegated(user_email)

请注意,我将查看有关您发布的链接的联系团队。奇怪的是他们如何向我记录工作区驱动器身份验证。


0
投票

我已经遇到同样的问题有一段时间了。用户非个性化已经到位。我现在的主要假设是,存在类似域配置问题的问题,导致谷歌文档无法通过服务帐户访问导出:某些域暴露了该问题,而其他域则工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.