Getting started overview
Google Slides is a web-based presentation program that allows users to create and edit presentations online while collaborating with other users in real time. While Google Slides itself is a user-facing application, developers can interact with its underlying data and functionality through the Google Slides API. This API enables programmatic access to presentations, allowing for automation of tasks such as creating new slides, modifying existing content, and integrating presentation data with other applications.
To begin using the Google Slides API, the process typically involves several key steps:
- Account Setup: Ensure you have a Google Account, which is free for personal use.
- Project Creation: Create a new project in the Google Cloud Console.
- API Enablement: Enable the Google Slides API and other necessary APIs within your Google Cloud project.
- Credential Generation: Obtain OAuth 2.0 client credentials (client ID and client secret) for authentication.
- Client Library Setup: Install a Google API client library in your preferred programming language.
- Authorization: Implement the OAuth 2.0 flow to authorize your application to access user data.
- First Request: Make a basic API call to demonstrate successful integration.
This guide focuses on these foundational steps to help developers make their first successful interaction with the Google Slides API.
Create an account and get keys
Access to the Google Slides API requires a Google Account and a project created within the Google Cloud Console. Follow these steps to set up your environment and obtain the necessary credentials:
1. Sign in to your Google Account
If you do not have a Google Account, you can create one. Personal Google Accounts are free and provide access to Google Slides and the Google Cloud Console. Business accounts are part of Google Workspace and offer additional features and administrative controls.
2. Create a Google Cloud Project
The Google Slides API is part of the Google Cloud ecosystem. All API interactions are managed through projects in the Google Cloud Console.
- Navigate to the Google Cloud Console.
- From the project selector dropdown at the top, click New Project.
- Enter a Project name and choose an optional Organization and Location.
- Click Create.
3. Enable the Google Slides API
Once your project is created, you must explicitly enable the Google Slides API for that project.
- In the Google Cloud Console, navigate to APIs & Services > Library.
- Search for "Google Slides API".
- Select the Google Slides API from the results.
- Click Enable. You may also need to enable the Google Drive API if your application will list or manage files in Google Drive.
4. Create OAuth 2.0 Client Credentials
The Google Slides API uses OAuth 2.0 for authentication and authorization. This involves obtaining a client ID and client secret.
- In the Google Cloud Console, navigate to APIs & Services > Credentials.
- Click Create Credentials > OAuth client ID.
- If prompted, configure your OAuth consent screen first. This screen informs users about your application requesting access to their Google Account.
- For Application type, select Web application for server-side applications, or Desktop app for local development. For this guide, we'll assume a desktop application for simplicity in the first request, but the general OAuth flow applies to web applications as well.
- Enter a Name for your OAuth client.
- For Desktop app, no further configuration is immediately needed for redirect URIs. For Web application, you will need to specify authorized JavaScript origins and authorized redirect URIs.
- Click Create.
- A dialog will display your Client ID and Client Secret. Download the JSON file containing these credentials (or copy them) and store them securely. This file will be named something like
client_secret_YOUR_CLIENT_ID.json.
Your first request
This section demonstrates how to make a basic API call to the Google Slides API using Python. This example creates a new blank presentation.
Prerequisites
- Python 3.6+ installed.
- The
client_secret_YOUR_CLIENT_ID.jsonfile downloaded in the previous step.
1. Install Google Client Library
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib
2. Write Python Code to Create a Presentation
Create a Python file (e.g., create_presentation.py) and add the following code. This script will prompt you to authorize access via your web browser the first time it runs.
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/presentations"]
def main():
"""Shows basic usage of the Slides API.
Prints the title and ID of a sample presentation.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
"client_secret_YOUR_CLIENT_ID.json", SCOPES
)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
try:
service = build("slides", "v1", credentials=creds)
# Create a new presentation
presentation_title = "My First API Presentation"
body = {
"title": presentation_title
}
presentation = service.presentations().create(body=body).execute()
print(f"Created presentation with ID: {presentation.get('presentationId')}")
print(f"Presentation URL: https://docs.google.com/presentation/d/{presentation.get('presentationId')}/edit")
except HttpError as err:
print(err)
if __name__ == "__main__":
main()
Important: Replace "client_secret_YOUR_CLIENT_ID.json" with the actual filename of your downloaded credentials JSON file.
3. Run the Code
python create_presentation.py
The first time you run this script, a browser window will open, prompting you to log in with your Google Account and grant permission for your application to access your Google Slides. After authorization, the script will print the ID and URL of the newly created presentation. A file named token.json will also be created, storing your refresh token for future use, preventing repeated browser authorizations.
Common next steps
After successfully making your first API call, consider these next steps to further develop your integration with the Google Slides API:
-
Explore API Reference: Review the Google Slides API reference documentation to understand available methods for managing presentations, pages, elements, and text.
-
Add Content to Presentations: Learn how to add slides, shapes, text boxes, images, and other elements to your presentations programmatically. The API allows for batch updates, which is efficient for making multiple changes.
-
Manage Permissions: Understand how to share presentations and manage access permissions using the Google Drive API's permissions methods, as Slides presentations are stored in Google Drive.
-
Integrate with Other Google Workspace APIs: Combine the Slides API with other Google Workspace APIs like Google Sheets (for data sources) or Google Docs (for content generation) to create more robust applications. The Google Workspace Developer Guide provides a comprehensive overview.
-
Implement Error Handling and Retry Logic: Incorporate robust error handling into your application, especially for transient network issues or API rate limits. The Google client libraries often include built-in retry mechanisms, but custom logic may be necessary.
-
Review Quotas and Pricing: Familiarize yourself with the Google Slides API usage limits and quotas to avoid unexpected interruptions or costs. Monitor your API usage in the Google Cloud Console.
Troubleshooting the first call
If your first API call encounters issues, consider the following common troubleshooting steps:
| Step | What to Do | Where to Check |
|---|---|---|
| Authentication Error | Ensure your client_secret_YOUR_CLIENT_ID.json file is correctly named and located in the same directory as your script, or provide the correct path. Verify that you have authorized the application through the browser flow. |
Google Cloud Console > APIs & Services > Credentials; Script directory |
| API Not Enabled | Confirm that the Google Slides API (and potentially Google Drive API if listing files) is enabled for your Google Cloud Project. | Google Cloud Console > APIs & Services > Library |
| Incorrect Scopes | Verify that the SCOPES variable in your code includes all necessary permissions. For creating presentations, https://www.googleapis.com/auth/presentations is typically sufficient. For broader access, use https://www.googleapis.com/auth/presentations.readonly for read-only or https://www.googleapis.com/auth/drive for full Drive access. |
Your Python script's SCOPES definition; OAuth 2.0 Scopes for Google APIs |
HttpError (e.g., 403 Permission Denied) |
This usually indicates an issue with authorization or permissions. Ensure the authenticated user has permission to perform the requested action (e.g., create a presentation in a specific folder, or access a specific presentation). Re-run the script to re-authorize if the token.json file might be stale or incorrect. |
Google Cloud Console > APIs & Services > Dashboard (for API errors); The user's Google Drive permissions |
| Network Issues | Check your internet connection. Temporary network interruptions can cause API calls to fail. | Local network connection |
| Client Library Version | Ensure your installed Google API client library versions are up to date. | pip list in your terminal; Google API Python Client PyPI page |