Getting Started with Google Slides API in PHP

29th December 2018

Automatically creating powerpoint files has a tonne of potential uses, from company front-of-house news displays, weekly sales reports, to live statistic feeds.

I spent a month working with the Google Slides API to programatically turn a meeting plan from a MySQL database into a powerpoint. It had layers of complexity such as adding styling (background images, fonts, colours), adding and scaling images, importing slides from an existing .pptx file amoung others.

Using Google Slides to programmatically generate powerpoint files is straight forward, but isnt without its pitfuls. It requires some setup, configuration and some understanding of how OAuth 2 works in the Google environment.

The following is what is required:

Create Google Cloud Account

Create Google Cloud project

Enable Google Cloud APIs:

Google Slides API

Setup OAuth

Initialised access tokens

Let's get started

Create Google Cloud Account

First up you will need to create a Google Cloud Account. The Google Cloud service is what is used to access Google APIs. The Google Account you use to create your Google Cloud Account will be tied together, that will mean you will need to use that account to login in the future.

Start by visiting the Google Cloudsite and create an account. Take note of this URL for later.

Create Google Cloud project

Next up you need to create a Google Cloud project. A project is where you API actions are executed against, projects can be moved to other accounts and there are API limits, abuse and other factors which are applied to your requests on a project level. You can create multiple projects, and switch between them.

So go ahead, create a new project and fill in all the information required. You can always go back and edit the URL information later. After your create the project it can take a couple minutes to complete, this is normal in my experience.

Enable Google Cloud APIs

Now you've create your project we need to enable the APIs that we want to use. In the Google Cloud you can access a variety of Google Cloud APIs such as Youtube, Docs, Maps, Calendar and much more. You'll find the API page hidden in the top-left hamburger menu under APIs & Services. Once you're there you'll see a bit blue link at the top of the page entitled "Enable APIs and services".

Once you're there you'll be able to search to enable the APIs. In our case we will just want to enable the Google Slides API.

Setup OAuth

In short, OAuth is a modern standard of web-based API security using token-based authentication. The details of how this works are beyond the scope of this article but you can find more information here and a great introductory video here. If you haven't used OAuth before, I'd strongly suggest spending a few minutes understanding it, otherwise you're not going to have a good time.

The task here is to setup OAuth settings which will tell Google where is valid for API requests to come from domain name wise. Additionally the information entered here is presented to the end user of the application.

To get started, whilst viewing your project click the main hamburger menu, select APIs & Services -> Credentials. Once you're on the credentials page, select Create Credentials, you'll want to use the selectable type OAuth Client ID, then use Web application in the following screen. The domain names listed here need to be real domain names used by your application. For the sake of this tutorial we'll be operating the API over the command line, so just use localhost as the domain.

Fill out all the information it asks as best as you can. You can always revisit this stage later.

Retain the OAuth config files for later. At this point setup is done, let's get coding.

Get authenticated with code

We're going to be working off of the base example Google provide in the form of an official sample library. You can find their full code on their Github repo. We're also going to be using composer - the PHP defacto dependency management tool, if you don't know what that is, pause for 5 minutes to install it and read up on it on their official site.

First install the official PHP library as a dependency in your project by running this on the command line:

composer require google/apiclient:^2.0

Copy the credentials.json file you downloaded in the previous step into the current directory. Then create a slides_tutorial_01.php file. Your project directory should look like this:

vendor/
credentials.json
slides_tutorial_01.php

Add the following to your slides_tutorial_01_authenticate.php:

// create a google client
$client = new Google_Client();
// give the application a friendly name (for your benifit only)
$client->setApplicationName('Google Slides API PHP Quickstart');
/**
* this is the scope the application needs - the scopes defined here cannot exceed the scopes
* set in the `Enable Google Cloud APIs` step.
*/
$client->setScopes(Google_Service_Slides::PRESENTATIONS_READONLY);
// this is the file path to the `credentials.json` in our directory
$client->setAuthConfig('credentials.json');
/**
* by setting offline this will return an oauth token which will never expire (a refresh token)
* which will enable us to get more access tokens without require the user to accept again
*/
$client->setAccessType('offline');
$client->setPrompt('select_account consent');

Add the following to the end of your existing file, read through the inline comments yourself to get a better understanding of what's happening here. This essentially is taking care of authenticating, and will require some interaction via command line inputs:

At this point, you're good to run the file and see how authentication flows! This is a rough rundown of what happens:

Application loads the configuration file

Application checks if an auth token has already been set

Application generates the required URL to get auth token, then presents it on the command line

User (you!) copy-paste that into your web browser

You login with whatever Google account you want to run the Google Slides request against, and accept the API scope/auth the application

You copy the token from the URL bar

You past the token back into your command line application

The application does some interaction with Google's servers to get access tokens and save them to a new tokens.json file, which is reused for future requests

At this point you finally have everything needed to make real requests against the Google Slides API.

Creating a Google Slide presentation

First up, we're going to create a new slide presentation. This should give you a firm understanding on how the API works, once you understand the pattern everything else can be understood from the Google Slides documentation.

We're going to create a new file called slides_tutorial_02_create_slide.php:

If you run this you'll find that a new presentation is created, then a new slide is added to that presentation.

The basic idea here, is that you build up a series of "requests", then requests are sent in a batch to Google, and an itterable of responses are returned.

And that's it! There's a lot more to learn, you'll find all the best information on the official Google PHP Slides documentation site. If there is anything you think I could help with, why not drop me a tweet?