在复制的文档中应用原始注释

Apply original comments in a copied document

我正在尝试使用 Google 驱动器 api 制作一份带有注释的文档副本。复制本身成功,但创建的复制文档没有任何注释。我试图遍历原始文档中的所有评论并将其应用于复制的文档以克服这个问题,这确实适用于所有评论的所有内容,但我是评论的作者。我希望原作者也是副本中评论的作者。

我知道这种行为可能是需要的,否则我可以添加似乎来自其他人的评论,但是如果我在 google 驱动器中打开任何带有评论的文档,就会有一个文件 -> 制作具有复制所有评论的复选框的复制选项:

如果我这样做,将创建一个新副本,并添加来自原作者的评论(并附注说明评论是从原始文档复制的)。有什么方法可以从 API?

做到这一点

首先,回答你对你发表的评论的疑惑。是的,所有语言都将 REST 调用包装到 API.

因此,我正在使用 Try this API 检查驱动器 API,但无法按照您的意愿复制评论。

因此,作为一种解决方法(这就是为什么我问你使用的语言,以便能够制作示例代码),你可以使用 服务帐户 来冒充您想要的任何用户,这样,评论将被注册,就像 he/she 那样。

我会列出一系列链接,帮助您设置服务帐户,然后才能使用它。

1) Enable APIs 你想要的(在这种情况下只有驱动器 API)

2) Create the service account and credentials.

3) Delegate domain-wide authority to your service.

4) 然后你可以使用下面的代码来生成一个新的评论作为你正在模拟的用户:

from googleapiclient import discovery, errors
from httplib2 import Http
from oauth2client import file, client, tools
from google.oauth2 import service_account

SERVICE_ACCOUNT_FILE = 'service_account.json'
SCOPES = ['https://www.googleapis.com/auth/drive']

# The user we want to "impersonate"
USER_EMAIL = "impersonated-user@your-domain.com"

# Set the credentials using the .json and the SCOPES
credentials = service_account.Credentials.\
    from_service_account_file(SERVICE_ACCOUNT_FILE, scopes= SCOPES)
delegated_credentials = credentials.with_subject(USER_EMAIL)

try:
    # Insert the comment
    service = discovery.build('drive', 'v3', credentials=delegated_credentials)
    service.comments().create(fileId="your file id", fields="*", body={
        "content": "TESTTT"
    }).execute()

except errors.HttpError as err:
        print('\n---------------You have the following error-------------')
        print(err)
        print('---------------You have the following error-------------\n')

通知

仅当您拥有 G Suite 帐户并且拥有管理员访问权限时,服务帐户才可用。

文档

为了构建我传递给你的代码,我从这些主题中得到了帮助: