Search code examples
python-3.xgoogle-apigoogle-slides-api

Google Slides API: no "client_secret.json"


I'm new to Google Slides API and am trying to build a slide deck for daily news headlines by replacing image and text placeholders (for your reference, see https://www.youtube.com/watch?v=8LSUbKZq4ZY and http://wescpy.blogspot.com/2016/11/using-google-slides-api-with-python.html).

But when I try to run my modified program, I get an error message that says no file or directory exists called "client_secret.json" (which is included in the API tutorial's code). The tutorial code is from 2 years ago so I'm not sure if there's been any updates in the Google Slides API, but I'd really appreciate help on navigating this issue. Below is my code (note: "scraped list" is a list of dictionaries, with each dictionary containing a value for keys "headline" and "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')

Solution

  • Never used python google api but error indicates that you dont have your 'client_secret.json' file or it is in wrong place.

    Scenario 1 - you dont have 'client_secret.json' file

    This file is used by API to automatically verify that you are you. With this all API calls are made by your behalf. To get this file:

    • go to Google API console
    • open your project (or create new one)
    • click "Enable APIs and services" to find and enable Google Slides API
    • click "Credentials" in left menu, and then "Create credentials" -> "oAuth client ID"
    • choose Web application, accept all windows
    • now you should see new credentials on list, you can click on them and there will be button on top menu named "download JSON", there you will obtain your credentials (which by name are secret so keep them somewhere safe)

    Scenario 2 - your 'client_secret.json' file is in wrong place

    In this case I can't be very helpful, just try to inspect library to know where it looks for file and put it there (library directory, project root directory, hard to tell).

    Let me know if it worked, as Google APIs and their libraries sometimes acts unexpectedly.