Google幻灯片API:没有"client_secret.json"

Google Slides API: no "client_secret.json"

我是 Google 幻灯片 API 的新手,我正在尝试通过替换图像和文本占位符来为每日新闻标题构建幻灯片平台(供您参考,请参阅 https://www.youtube.com/watch?v=8LSUbKZq4ZY and http://wescpy.blogspot.com/2016/11/using-google-slides-api-with-python.html ).

但是当我尝试 运行 我修改后的程序时,我收到一条错误消息,指出不存在名为 "client_secret.json" 的文件或目录(包含在 API 教程的代码中).教程代码是 2 年前的,所以我不确定 Google 幻灯片 API 中是否有任何更新,但我非常感谢您帮助解决这个问题。下面是我的代码(注意:"scraped list" 是一个字典列表,每个字典包含键值 "headline" 和 "imgURL"。)

from __future__ import print_function
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
from datetime import date

from scrapef2 import scrape

scrapedlist = scrape()

TMPLFILE = 'CrimsonTemplate'   # use your own!
SCOPES = (
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/presentations',
)
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
HTTP = creds.authorize(Http())
DRIVE  = discovery.build('drive',  'v3', http=HTTP)
SLIDES = discovery.build('slides', 'v1', http=HTTP)

rsp = DRIVE.files().list(q="name='%s'" % TMPLFILE).execute().get('files')[0]
DATA = {'name': '[DN] '+ str(date.today())}
print('** Copying template %r as %r' % (rsp['name'], DATA['name']))
DECK_ID = DRIVE.files().copy(body=DATA, fileId=rsp['id']).execute().get('id') # TO DO: How to copy into a specific folder

for i in range(3):
    print('** Get slide objects, search for image placeholder')
    slide = SLIDES.presentations().get(presentationId=DECK_ID,
            fields='slides').execute().get('slides')[i]
    obj = None
    for obj in slide['pageElements']:
        if obj['shape']['shapeType'] == 'RECTANGLE':
            break

    print('** Replacing placeholder text and icon')
    reqs = [
        {'replaceAllText': {
            'containsText': {'text': '{{Headline}}'},
            'replaceText': scrapedlist[i]["headline"]
        }},
        {'createImage': {
            'url': scrapedlist[i]["imgURL"],
            'elementProperties': {
                'pageObjectId': slide['objectId'],
                'size': obj['size'],
                'transform': obj['transform'],
            }
        }},
        {'deleteObject': {'objectId': obj['objectId']}},
    ]
    SLIDES.presentations().batchUpdate(body={'requests': reqs},
            presentationId=DECK_ID).execute()
    print('DONE')

从未使用过 python google api 但错误表明您没有 'client_secret.json' 文件或文件位置错误。

情况 1 - 您没有 'client_secret.json' 文件

此文件被API用来自动验证您的身份。这样一来,所有 API 电话都是由您代为拨打的。要获取此文件:

  • 转到Google API console
  • 打开您的项目(或创建新项目)
  • 单击 "Enable APIs and services" 查找并启用 Google 幻灯片 API
  • 点击左侧菜单中的"Credentials",然后"Create credentials" -> "oAuth client ID"
  • 选择Web应用,全部接受windows
  • 现在您应该会在列表中看到新的凭据,您可以点击它们,顶部菜单上会出现一个名为 "download JSON" 的按钮,在那里您将获得您的凭据(名称是秘密的,因此请将它们保存在某处安全)

情况 2 - 您的 'client_secret.json' 文件位置错误

在这种情况下我帮不了什么忙,只是尝试检查库以了解它在哪里寻找文件并将其放在那里(库目录,项目根目录,很难说)。

让我知道它是否有效,因为 Google APIs 和他们的图书馆有时会出乎意料。