无法使用 google 驱动器 api 复制 google 幻灯片文件

unable to copy a google slide file using google drive api

我想复制 google 驱动器中现有的模板 ppt。然后我想将占位符文本更改为其他文本。

这就是我正在尝试的。

from google.oauth2 import service_account
import googleapiclient.discovery


SCOPES = (
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/presentations',
)
SERVICE_ACCOUNT_FILE = 'cred.json'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES) 

SLIDES = discovery.build('slides', 'v1', credentials=credentials)
DRIVE  = discovery.build('drive',  'v3', credentials=credentials)

TMPLFILE = 'title slide template' 

rsp = DRIVE.files().list(q="name='%s'" % TMPLFILE).execute().get('files')[0]
print(rsp)
DATA = {'name': 'Google Slides API template DEMO'}
print('** Copying template %r as %r' % (rsp['name'], DATA['name']))
DECK_ID = DRIVE.files().copy(body=DATA, fileId=rsp['id']).execute().get('id')
print(DECK_ID)

print('** Replacing placeholder text')

reqs = [
    {'replaceAllText': {
        'containsText': {'text': '{{text}}'},
        'replaceText': final_til[0]
    }},
]

SLIDES.presentations().batchUpdate(body={'requests': reqs},
        presentationId=DECK_ID).execute()
print('DONE')

但它不起作用。我没有收到任何错误。一切正常,但我没有看到新的 ppt。

输出:

{'kind': 'drive#file', 'id': '15mVjkrT7PkckKetK_q9aYRVxaDcwDdHpAh7xjrAWB6Q', 'name': 'title slide template', 'mimeType': 'application/vnd.google-apps.presentation'}  <--- rsp
** Copying template 'title slide template' as 'Google Slides API template DEMO'
11O97tySSNaboW6YRVD62Q7HLs8aVuS2pWyLYXImdSec   <-- DECK_ID
** Replacing placeholder text
DONE

如果我改变

SLIDES.presentations().batchUpdate(body={'requests': reqs},
            presentationId=DECK_ID).execute()

SLIDES.presentations().batchUpdate(body={'requests': reqs},
            presentationId=rsp.get('id')).execute()

然后它确实替换了我不想要的模板文件中的文本。

为什么会这样?

我相信你的现状和目标如下。

  • 根据您的脚本,
    • 您正在为 python 使用 googleapis。
    • 您已经可以使用服务帐户使用云端硬盘 API 和幻灯片 API。
    • an existing template ppt present 是一个 Google 幻灯片,不是 PowerPoint 文件。

修改点:

  • 根据您的脚本和 But it is not working. I don't get any error. everything works fine but I don't see the new ppt.,我了解到您可能希望在您的 Google 驱动器上查看由服务帐户复制的 Google 幻灯片。

    • 当 Google 幻灯片被服务帐户复制时,复制的 Google 幻灯片被放入服务帐户的驱动器。服务帐户的驱动器与您的 Google 驱动器不同。这样,您就无法在您的云端硬盘上看到复制的 Google 幻灯片。我认为这可能是您遇到问题的原因。
  • 为了在您的 Google 驱动器上查看服务帐户复制的 Google 幻灯片,例如,可以使用以下解决方法。

    1. 与您的 Google 帐户的电子邮件共享复制的 Google 幻灯片。
      • 此时,您可以在共享文件夹中看到共享文件。
    2. 首先,它会在您的 Google 驱动器中创建新文件夹,并与服务帐户的电子邮件共享该文件夹。复制 Google 幻灯片时,它会将共享文件夹设置为目标文件夹。

解决方法 1:

在此解决方法中,它与您的 Google 帐户的电子邮件共享复制的 Google 幻灯片。当这反映到你的脚本中时,它变成如下。

修改后的脚本:

在这种情况下,使用“权限:创建”为复制的文件创建新权限。

从:
print(DECK_ID)

print('** Replacing placeholder text')
到:
print(DECK_ID)

permission = {
    'type': 'user',
    'role': 'writer',
    'emailAddress': '###@gmail.com'  # <--- Please set the email of your Google account.
}
DRIVE.permissions().create(fileId=DECK_ID, body=permission).execute()

print('** Replacing placeholder text')

解决方法 2:

在此解决方法中,Google 幻灯片被复制到您的 Google 驱动器中的共享文件夹。在使用此脚本之前,请创建新文件夹并与服务帐户的电子邮件共享该文件夹。当这反映到你的脚本中时,它变成如下。

修改后的脚本:

在这种情况下,元数据被添加到 DRIVE.files().copy() 的请求正文中。

从:
DATA = {'name': 'Google Slides API template DEMO'}
到:
DATA = {'name': 'Google Slides API template DEMO', 'parents': ['###']}
  • 请将共享文件夹的文件夹ID设置为###

参考文献:

@Tanaike 的回答很好,但还有另一种选择:

模拟帐户

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)
        
delegated_credentials = credentials.with_subject(<email>)

DRIVE = build('drive','v3', credentials = delegated_credentials)

这里有一个很好的概述:Using OAuth 2.0 for Server to Server Applications, specifically this section 遍历代码。

记得在 GCP 控制台管理控制台中设置域范围委派

在 GCP Cloud 控制台中初始化的项目也已从管理控制台 > 安全 > API 控制 > 域范围委派 > 添加新的范围内授予范围

脚本做的第一件事是使用 from_service_account_file:

构建凭据
credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

然后它构建 delegated 凭据,即要模拟的用户:

delegated_credentials = credentials.with_subject('<EMAIL>')

它可以从那里正常构建服务。您可以像用户自己做的那样保存到用户的驱动器。

参考资料