Skip to content

Commit 8254cec

Browse files
google docs connector (#1676)
1 parent 16fb3a1 commit 8254cec

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

docs/google.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,61 @@ API
260260
.. autoclass :: parsons.google.google_civic.GoogleCivic
261261
:inherited-members:
262262
263+
*************
264+
Google Docs
265+
*************
266+
267+
========
268+
Overview
269+
========
270+
271+
The GoogleDocs class allows you to interact with Google Docs. You can use this connector to access and manipulate Google Docs documents programmatically.
272+
273+
In order to instantiate the class, you must pass Google service account credentials as a dictionary, or store the credentials as a JSON file locally and pass the path to the file as a string in the ``GOOGLE_DRIVE_CREDENTIALS`` environment variable. You can follow these steps:
274+
275+
- Go to the `Google Developer Console <https://console.cloud.google.com/apis/dashboard>`_ and make sure the "Google Docs API" and "Google Drive API" are both enabled.
276+
- Go to the credentials page via the lefthand sidebar. On the credentials page, click "create credentials".
277+
- Choose the "Service Account" option and fill out the form provided. This should generate your credentials.
278+
- Select your newly created Service Account on the credentials main page.
279+
- select "keys", then "add key", then "create new key". Pick the key type JSON. The credentials should start to automatically download.
280+
281+
You can now copy and paste the data from the key into your script or (recommended) save it locally as a JSON file.
282+
283+
==========
284+
Quickstart
285+
==========
286+
287+
To instantiate the GoogleDocs class, you can either pass the constructor a dict containing your Google service account credentials or define the environment variable ``GOOGLE_DRIVE_CREDENTIALS`` to contain a path to the JSON file containing the dict.
288+
289+
.. code-block:: python
290+
291+
from parsons import GoogleDocs
292+
293+
# First approach: Use API credentials via environmental variables
294+
docs = GoogleDocs()
295+
296+
# Second approach: Pass API credentials as argument
297+
credential_filename = 'google_drive_service_credentials.json'
298+
docs = GoogleDocs(app_creds=credential_filename)
299+
300+
You can then use the client to interact with Google Docs:
301+
302+
.. code-block:: python
303+
304+
# Access the Google Docs API client
305+
document_id = 'your-document-id'
306+
document = docs.client.documents().get(documentId=document_id).execute()
307+
308+
# Get the document content
309+
content = document.get('body').get('content')
310+
311+
===
312+
API
313+
===
314+
315+
.. autoclass :: parsons.google.google_docs.GoogleDocs
316+
:inherited-members:
317+
263318
*************
264319
Google Drive
265320
*************

parsons/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
("parsons.google.google_civic", "GoogleCivic"),
6969
("parsons.google.google_cloud_storage", "GoogleCloudStorage"),
7070
("parsons.google.google_drive", "GoogleDrive"),
71+
("parsons.google.google_docs", "GoogleDocs"),
7172
("parsons.google.google_sheets", "GoogleSheets"),
7273
("parsons.hustle.hustle", "Hustle"),
7374
("parsons.mailchimp.mailchimp", "Mailchimp"),

parsons/google/google_docs.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import logging
2+
import uuid
3+
from typing import Optional, Union
4+
5+
from google.oauth2.credentials import Credentials
6+
from googleapiclient.discovery import build
7+
8+
from parsons.google.utilities import (
9+
load_google_application_credentials,
10+
setup_google_application_credentials,
11+
)
12+
13+
logger = logging.getLogger(__name__)
14+
15+
16+
class GoogleDocs:
17+
"""
18+
A connector for Google Docs
19+
20+
`Args:`
21+
app_creds: dict | str | Credentials
22+
Can be a dictionary of Google Drive API credentials, parsed from JSON provided
23+
by the Google Developer Console, or a path string pointing to credentials
24+
saved on disk, or a google.oauth2.credentials.Credentials object. Required
25+
if env variable ``GOOGLE_DRIVE_CREDENTIALS`` is not populated.
26+
"""
27+
28+
def __init__(
29+
self,
30+
app_creds: Optional[Union[str, dict, Credentials]] = None,
31+
):
32+
scopes = [
33+
"https://www.googleapis.com/auth/documents",
34+
"https://www.googleapis.com/auth/drive",
35+
]
36+
37+
if isinstance(app_creds, Credentials):
38+
credentials = app_creds
39+
else:
40+
env_credentials_path = str(uuid.uuid4())
41+
setup_google_application_credentials(
42+
app_creds, target_env_var_name=env_credentials_path
43+
)
44+
credentials = load_google_application_credentials(env_credentials_path, scopes=scopes)
45+
46+
self.client = build(
47+
"docs",
48+
"v1",
49+
credentials=credentials,
50+
cache_discovery=False,
51+
)

0 commit comments

Comments
 (0)