You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用服务账号调用Google Drive API共享文件夹遇invalidSharingRequest错误

关于Google Drive API服务账号共享文件夹报错的问题

我尝试通过服务账号调用Google Drive API共享文件夹,使用的Python代码如下:

import apiclient
import httplib2
from googleapiclient import discovery
from googleapiclient.errors import HttpError
from oauth2client.service_account import ServiceAccountCredentials

CREDENTIALS_FILE = 'creds.json'

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    CREDENTIALS_FILE,
    ['https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/drive']
)
httpAuth = credentials.authorize(httplib2.Http())
DRIVE = discovery.build('drive', 'v3', http=httpAuth)

def share_file(file_id, user_email):
    """Batch permission modification.
    Args:
        file_id: file Id
        user_email: User ID
    Prints modified permissions
    """
    ids = []
    file_id = file_id

    def callback(request_id, response, exception):
        if exception:
            # Handle error
            print(exception)
        else:
            print(f'Request_Id: {request_id}')
            print(F'Permission Id: {response.get("id")}')
            ids.append(response.get('id'))

    batch = DRIVE.new_batch_http_request(callback=callback)
    user_permission = {
        'type': 'user',
        'role': 'commenter',
        'emailAddress': user_email,
        'supportsAllDrives': True
    }
    batch.add(DRIVE.permissions().create(fileId=file_id,
                                         body=user_permission,
                                         fields='id'
                                         ))
    batch.execute()

    return ids

执行后返回错误信息:

Details: "[{'domain': 'global', 'reason': 'invalidSharingRequest', 'message': 'Bad Request. User message: "You cannot share this item because it has been flagged as inappropriate."'}]"

请问我哪里操作有误?


问题分析与解决

这个错误和代码逻辑无关,问题出在目标文件夹本身:

  • 该文件夹被Google系统标记为违规内容(比如包含违反政策的文件、版权侵权内容等),因此被限制了共享功能
  • 服务账号的权限配置(你使用的drive和drive.file权限足够完成共享操作)、代码中的参数(如supportsAllDrives=True、权限类型与角色设置)均无问题

解决步骤:

  1. 登录服务账号对应的Google Drive(注意服务账号拥有独立的Drive空间,并非个人Drive),检查目标文件夹内的内容是否违反Google Drive政策
  2. 若确认内容合规,可通过Google申诉渠道提交审核,解除共享限制
  3. 若内容确实违规,需清理违规文件后再尝试共享

内容的提问来源于stack exchange,提问作者VanDok_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 11:01:58