【问题标题】:Google Slides API: no "client_secret.json"谷歌幻灯片 API:没有“client_secret.json”
【发布时间】:2018-11-28 07:08:46
【问题描述】:

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

但是当我尝试运行修改后的程序时,我收到一条错误消息,指出不存在名为“client_secret.json”的文件或目录(包含在 API 教程的代码中)。教程代码来自 2 年前,所以我不确定 Google Slides 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-3.x google-api google-slides-api


    【解决方案1】:

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

    场景 1 - 你没有 'client_secret.json' 文件

    API 使用此文件自动验证您的身份。有了这个,所有 API 调用都由您代表进行。要获取此文件:

    • 转至Google API console
    • 打开您的项目(或创建新项目)
    • 点击“启用 API 和服务”以查找并启用 Google Slides API
    • 单击左侧菜单中的“凭据”,然后单击“创建凭据”->“oAuth 客户端 ID”
    • 选择 Web 应用程序,接受所有窗口
    • 现在您应该会在列表中看到新的凭据,您可以单击它们,顶部菜单上会出现一个名为“下载 JSON”的按钮,在那里您将获得您的凭据(名称是保密的,因此请将它们保存在安全的地方)

    场景 2 - 您的“client_secret.json”文件位置错误

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

    让我知道它是否有效,因为 Google API 及其库有时会出现意外行为。

    【讨论】:

    • 谢谢。按照这些步骤,我生成了一个 JSON,但不是 here 中描述的格式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    相关资源
    最近更新 更多